xlwt 和xlrd 读取写入表格

一个简单的实例 解释 python读取表格和保存写入表格

xlwt写入表格 可以写样式  但是不好的地方就是 列需要用数字来表示  

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")


0 评论

发表评论