python 复制表格并填入数据
这里用到三个库 xlutils xlrd xlwt
pip install xlrd -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com #表格读取
pip install xlwt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com #表格保存
pip install xlutils -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com #表格格式复制
# 编写时间:2022-02-08 --9:44 # coding=utf-8 from xlutils.copy import copy #xlutils 是用于excel格式引用的库 import xlrd #xlrd 表格读取库 import xlwt #xlrd 表格写入库 tem_excel=xlrd.open_workbook("C:\\Users\\Administrator\\Desktop\\3.xls",formatting_info=True) #打开表格 tem_sheet=tem_excel.sheet_by_index(0) #读取打开表格的指定的表 new_excel=copy(tem_excel) #内存创建一张表 复制tem_excel数据 new_sheet=new_excel.get_sheet(0) # 获取复制表格的sheet #开始设置格式 style=xlwt.XFStyle() #设置字体 font=xlwt.Font() font.name="华文中宋" font.bold=True font.height=720 style.font=font #将设置的字体赋值给样式 #设置边框 borders=xlwt.Borders() borders.top=xlwt.Borders.THIN borders.bottom=xlwt.Borders.THIN borders.left=xlwt.Borders.THIN borders.right=xlwt.Borders.THIN style.borders=borders #将设置的表格赋值给样式 #设置居中情况 alignment=xlwt.Alignment() alignment.horz=xlwt.Alignment.HORZ_CENTER alignment.vert=xlwt.Alignment.VERT_CENTER style.alignment=alignment #将设置的居中参数赋值给样式 new_sheet.write(0,1,"111",style) #写入单元格信息 参数分别为 行 列 内容 样式 new_excel.save("C:\\Users\\Administrator\\Desktop\\4.xls") #保存新的excel路径
excel 读取数据 进行简单计算后 复制文件 写入计算的值
# 编写时间:2022-02-08 --10:48 # coding=utf-8 import xlwt import xlrd from xlutils.copy import copy #打开xlsx文件并确定数据表 xlsx=xlrd.open_workbook("C:\\Users\\Administrator\\Desktop\\5.xls") table=xlsx.sheet_by_index(0) rownum=table.nrows #获取所有数据 all_data=[] #定义一个列表 for n in range(1,rownum): company=table.cell(n,1).value price=int(table.cell(n,3).value) #这里是两种获得单元格值的方式 weight=int(table.cell_value(n,4)) zong=price*weight data={'row':n,'company':company,'weight':weight,'price':price,'zong':zong} all_data.append(data) #将每行设置的字典加入列表 new_xls=copy(xlsx) new_sheet=new_xls.get_sheet(0) print(all_data) n=0 for v in all_data: new_sheet.write(all_data[n]['row'],2,all_data[n]['zong']) n=n+1 new_xls.save("C:\\Users\\Administrator\\Desktop\\6.xls")
将D盘文件写入表格并保存
# 编写时间:2022-02-08 --14:41 # coding=utf-8 import os import xlwt file_dir='d:/' names=os.listdir(file_dir) #获取所有名称 new_workbook=xlwt.Workbook() #新建一个表格文件 worksheet=new_workbook.add_sheet('new_test') #增加一个表格 n=0 for i in names: worksheet.write(n,0,i) 循环写入 n+=1 new_workbook.save('d:/2.xls') #保存
另外两个新建表格使用的库
pip install xlsxwriter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com #可以超过256列 但是不能带格式
pip install openpyxl -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com #性能稍微不稳定
以下是xlsxwriter使用实例
关于xlsxwriter 的手册
http://mrdoc.zmister.com/project-18/doc-44/
https://www.wenjiangs.com/docs/python-xlsxwriter
https://blog.csdn.net/xixihahalelehehe/article/details/105617256
# 编写时间:2022-02-08 --11:50 # coding=utf-8 import xlsxwriter as xw workbook = xw.Workbook('d:/number.xlsx') # xlsx格式 sheet0 = workbook.add_worksheet('new') #设置一张表 for i in range(0, 300): sheet0.write(0, i, i) # 行 列 内容 workbook.close()
openpyxl的实例:
csdn上关于openpyxl的手册:
https://blog.csdn.net/kongsuhongbaby/article/details/85646750
https://blog.csdn.net/qq_38830593/article/details/102870762
# 编写时间:2022-02-08 --11:50 # coding=utf-8 import openpyxl as op workbook = op.load_workbook('d:/number.xlsx') # 加载excel文件 sheet0 = workbook['new'] # 打开名为 new的数据表格 sheet0['A1'] = '222' sheet0['B2'] = '444' n=0 for g in range(1,10): n=n+1 b="A"+str(n) sheet0[b]="填入单元格" workbook.save('d:/2.xlsx')
评论列表
发表评论