使用python自动执行某些任务并最终写入现有的电子表格。我使用的是xlwt,xlrd和xlutils模块。
所以我设置它的方法是打开文件,复制,写入,然后将其保存回同一文件。当我执行最后一步时,将删除所有excel格式,例如注释和图表。有办法吗?我认为它与excel对象有关。
谢谢
示例代码
import xlwt
import os
import xlrd, xlutils
from xlrd import open_workbook
from xlutils.copy import copy
style1 = xlwt.easyxf('font: name Calibri, color-index black, bold off; alignment : horizontal center', num_format_str ='###0')
script_dir = os.path.dirname('_file_')
Scn1 = os.path.join(script_dir, "\sample\Outlet.OUT")
WSM_1V = []
infile = open (Scn1, "r")
for line in infile.readlines():
WSM_1V.append(line [-10:-1])
infile.close()
Existing_xls = xlrd.open_workbook(r'\test\test2.xls', formatting_info=True, on_demand=True)
wb = xlutils.copy.copy(Existing_xls)
ws = wb.get_sheet(10)
for i,e in enumerate(WSM_1V,1):
ws.write (i,0, float(e),style1)
wb.save('test2.xls')