xlwt 和xlrd 读取写入表格

磊落不羁 by:磊落不羁 分类:办公自动化 时间:2年前 阅读:83 评论:0

一个简单的实例 解释 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")


非特殊说明,本文版权归原作者所有,转载请注明出处

本文地址:http://php.liulei.com.cn/?type=acticle&id=30

评论列表

发表评论

  • 昵称(必填)
  • 邮箱
  • 网址

TOP