Python写入(xlwt)到现有的Excel工作表,删除图表和格式

我正在使用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') 

使用这些软件包,没有办法丢失评论和图表,以及许多其他工作簿function。 xlrd软件包根本不会读取它们,而xlwt软件包根本就不会写入它们。 xlutils只是其他两个软件包之间的桥梁; 它不能读取xlrd无法读取的任何内容,也不能写入任何xlwt无法写入的内容。

为了达到你想达到的目标,可能你最好的select是自动运行一个Excel实例; 最好的Python软件包是xlwings ,它可以在Windows或Mac上运行。

你可以用win32com来做这个吗?

 from win32com import client ... xl = client.Dispatch("Excel.Application") wb = xl.Workbooks.Open(r'\test\test2.xls') ws = wb.Worksheets[10] for i,e in enumerate(WSM_1V,1): ws.Cells[i][0].Value = float(e) wb.save wb.Close() xl.Quit() xl = None