3月30日 【更改写入EXCEL】
# -* - coding: UTF-8 -* -
#create by henryzkf 20180330 succ
import xlrd,xlwt,xlutils,os
from xlrd import open_workbook
from xlutils.copy import copy
def modify_excel(filename):
book = xlrd.open_workbook(filename)
new_book = copy(book) # 复制excel
sheet = new_book.get_sheet(0) # 获取第一个表格的数据
sheet.write(0, 1, '123') # 修改0行1列的数据为'Haha'
new_book.save('secondsheet.xls') # 保存新的excel
os.remove(filename) # 删除旧的excel
os.rename('secondsheet.xls', filename) # 将新excel重命名'''
if __name__ == '__main__':
modify_excel('firstsheet1.xls')
前面使用从数据库取数写入没有问题,但是这次写入老是报错,
AttributeError: 'dict_items' object has no attribute 'sort'
解决措施:最后按照论坛建议找到目录下的:
Python35-32\Lib\site-packages\xlwt-1.0.0-py3.5.egg\xlwt 下的文件style.py
将报错处的alist.sort()改成sorted(alist)即可,不知道后期会不会影响其他的,有三个地方我都改了。
浙公网安备 33010602011771号