由于在工作中使用的是Windows 7操作系统,受支持的Python版本有限,这里下载了Python 3.8.10.exe,又去PyPi网站上下载了相关的第三方库,这里是xlrd、xlwt 和 xlutils库。
- xlrd-2.0.1-py2.py3-none-any.whl
- xlwt-1.3.0-py2.py3-none-any.whl
- xlutils-2.0.0-py2.py3-none-any.whl
先安装Python,然后使用 pip install <将待安装的库直接拖到此处> ,回车即可。需要注意的是,须先将xlrd、xlwt 库安装好,才能安装xlutils库。
示例:
from datetime import datetime
import xlrd,xlwt
from xlutils.copy import copy
inCalc=input("\n请输入Excel表格的全称【例:号码数据.xls】:")
# 获取工作簿对象
Input_wb=xlrd.open_workbook(inCalc) # 存储着 号码、命令模板
# 获取工作表对象
SJ_ws=Input_wb.sheet_by_name("输入数据")
MB_ws=Input_wb.sheet_by_name("网管模板")
nR=SJ_ws.nrows
TiHuan=['领','寺','罢','而','留','屋','的','散','起','意','室','就','分','秒']
HML=[]
for i in range(1,nR):
ShuJu=SJ_ws.row_values(i)
if (ShuJu[6]=="" and ShuJu[4]==""):
MoBan=MB_ws.col_values(0)
for mb in MoBan[1:]:
j=0
for sj in ShuJu:
mb=mb.replace(TiHuan[j],sj)
j=j+1
HML.append(mb)
HML.append("")
HML.append("")
elif (ShuJu[6]=="" and ShuJu[4]!=""):
MoBan=MB_ws.col_values(1)
for mb in MoBan[1:]:
j=0
for sj in ShuJu:
mb=mb.replace(TiHuan[j],sj)
j=j+1
HML.append(mb)
HML.append("")
HML.append("")
elif (ShuJu[6]!="" and ShuJu[4]==""):
MoBan=MB_ws.col_values(2)
for mb in MoBan[1:]:
j=0
for sj in ShuJu:
mb=mb.replace(TiHuan[j],sj)
j=j+1
HML.append(mb)
HML.append("")
HML.append("")
else:
MoBan=MB_ws.col_values(3)
for mb in MoBan[1:]:
j=0
for sj in ShuJu:
mb=mb.replace(TiHuan[j],sj)
j=j+1
HML.append(mb)
HML.append("")
HML.append("")
# 复制工作簿、新建工作表
Output_wb=copy(Input_wb)
SCML_ws=Output_wb.get_sheet("输出命令")
# 将生成的列表写到xls表格中
x=0
for hml in HML:
SCML_ws.write(x,0,hml)
x=x+1
# 获取时间戳
Timesuffixa= datetime.now().strftime(" %Y-%m-%d %H-%M-%S")
Timesuffix= datetime.now().strftime(" %m-%d %H-%M-%S")
# 输出工作簿
Output_wb.save('命令输出'+Timesuffix+'.xls')
print('\n\n恭喜!程序执行完毕!')
print('······')
print('······')
print('······')
print("\n提示输出文件:")
print(' 命令输出'+Timesuffix+'.xls\n')
效果如下所示:
参考资料:
- 无