以下代码实现将开通.xlsx、更换.xlsx两份表的部分内容汇总到通报.xlsx。
过程中也会有一些数据格式的变换和匹配其他信息,代码如下。
写代码不难,难在优化代码效率,所以,欢迎各位破友拍砖、指教,欢迎交流。
[Python] 纯文本查看 复制代码 import datetime
import time
from openpyxl import load_workbook
print("打开通报表")
toXlsx = load_workbook(filename='通报.xlsx')
toSheet = toXlsx['开通']
print("从通报表获得人员数据(用于匹配)")
toPersonnelSheet = toXlsx['个人统计']
toPersonnelSheetMaxRow = toPersonnelSheet.max_row
tupToColumnABC = toPersonnelSheet['A2':'C' + str(toPersonnelSheetMaxRow)]
# ### 第1份表 ###
print("打开开通源数据表")
formXlsx = load_workbook(filename='开通.xlsx', read_only=True)
formSheet = formXlsx['开通归档']
formSheetMaxRow = formSheet.max_row
print("获得开通源数据")
# 把要复制的列创建为元组
tupFormColumnD = formSheet['D2':'D' + str(formSheetMaxRow)]
tupFormColumnH = formSheet['H2':'H' + str(formSheetMaxRow)]
tupFormColumnI = formSheet['I2':'I' + str(formSheetMaxRow)]
tupFormColumnAU = formSheet['AU2':'AU' + str(formSheetMaxRow)]
tupFormColumnBB = formSheet['BB2':'BB' + str(formSheetMaxRow)]
print("写入开通数据")
for i in range(0, formSheetMaxRow - 1):
# 格式日期(把原来的日期yyyy/m/d H:M:S转换为yyyy-m-d)
thisDate = tupFormColumnAU[i][0].value.strftime('%Y-%m-%d')
# 判断业务类型
thisType = tupFormColumnH[i][0].value
thisTypeFormat = ""
if thisType == "手机宽带" or thisType == "手机宽带基础产品" or thisType == "宽带":
thisTypeFormat = "宽带"
elif thisType == "宽带电视基础产品":
thisTypeFormat = "电视"
elif thisType == "智能组网及增值收费服务基础产品":
thisTypeFormat = "组网"
elif thisType == "智慧管家基础安装服务":
thisTypeFormat = "安防"
elif thisType == "手机多媒体家庭电话属性产品":
thisTypeFormat = "电话"
elif thisType == "千兆":
thisTypeFormat = "千兆提速"
# 获得人员属性,根据人员名字,循环人员元组(toPersonnelSheetMaxRow),匹配出人员的区域(strRange)、来源(strAscription)
strName = tupFormColumnBB[i][0].value
strAscription = ""
strRange = ""
for row in range(0, toPersonnelSheetMaxRow - 1):
if tupToColumnABC[row][0].value == strName:
strAscription = tupToColumnABC[row][1].value
strRange = tupToColumnABC[row][2].value
break
# 并装
tupAddend = (tupFormColumnD[i][0].value, tupFormColumnH[i][0].value, tupFormColumnI[i][0].value,
tupFormColumnAU[i][0].value, tupFormColumnBB[i][0].value, thisDate, thisTypeFormat, strRange,
strAscription)
# 写入
toSheet.append(tupAddend)
print("开通数据写入完成,释放资源")
del tupFormColumnD, tupFormColumnH, tupFormColumnI, tupFormColumnAU, tupFormColumnBB
# 关闭开通源数据EXCEL表
formXlsx.close()
# ### 第2份表 ###
print("打开更换源数据表")
formXlsx = load_workbook(filename='更换.xlsx', read_only=True)
formSheet = formXlsx['历史工单']
formSheetMaxRow = formSheet.max_row
print("获得更换源数据")
# 把要复制的列创建为元组
tupFormColumnA = formSheet['A2':'A' + str(formSheetMaxRow)]
tupFormColumnBC = formSheet['BC2':'BC' + str(formSheetMaxRow)]
tupFormColumnDC = formSheet['DC2':'DC' + str(formSheetMaxRow)]
tupFormColumnEF = formSheet['EF2':'EF' + str(formSheetMaxRow)]
tupFormColumnHU = formSheet['HU2':'HU' + str(formSheetMaxRow)]
# 删除(清空)原有数据
toSheet = toXlsx['速率整改']
toSheetMaxRow = toSheet.max_row
toSheet.delete_rows(2, toSheet.max_row)
print("写入更换数据")
# 获得现在日期
strNow = time.localtime(time.time())
for i in range(0, formSheetMaxRow - 1):
# 如果tupFormColumnEF不为空
if tupFormColumnEF[i][0].value is not None:
# 如果tupFormColumnHU为是,且tupFormColumnEF(日期)的月份等于本月
if tupFormColumnHU[i][0].value == "是" and int(tupFormColumnEF[i][0].value.strftime('%m')) == strNow[1]:
# 格式日期(把原来的日期yyyy/m/d H:M:S转换为yyyy-m-d)
thisDate = tupFormColumnEF[i][0].value.strftime('%Y-%m-%d')
# 获得人员属性,根据人员名字,循环人员元组(toPersonnelSheetMaxRow),匹配出人员的区域(strRange)、来源(strAscription)
strName = tupFormColumnDC[i][0].value
strAscription = ""
strRange = ""
for row in range(0, toPersonnelSheetMaxRow - 1):
if tupToColumnABC[row][0].value == strName:
strAscription = tupToColumnABC[row][1].value
strRange = tupToColumnABC[row][2].value
break
# 并装
tupAddend = (tupFormColumnA[i][0].value, tupFormColumnBC[i][0].value, tupFormColumnDC[i][0].value,
tupFormColumnEF[i][0].value, thisDate, strRange, strAscription)
# 写入
toSheet.append(tupAddend)
# 更新报表日期
toNotificationSheet = toXlsx['通报统计']
localtime = yesterday = datetime.date.today() + datetime.timedelta(-1)
toNotificationSheet['C1'] = yesterday
# 保存目标EXCEL表
toXlsx.save('通报.xlsx')
# 关闭两个EXCEL表
formXlsx.close()
toXlsx.close()
|