用python修改.xls文件

2024-04-19 09:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

Im用python编写一个脚本,读取excel文件,然后对单元格进行更改,然后将所有内容复制到另一个excel文件并保存。在

例如,我有example.xls,然后生成example_copy.xls。在

每一次都是一样的,但我的问题是,表格上的公式不见了,当它复制时,它保留了所有的文本,但我也想复制所有的公式。在

我用xlutils来做这个。这是我代码的一部分:

def write_frecuencias(self):
    book = xlrd.open_workbook('Examen.xls', formatting_info=True)
    book_copy = copy(book)
    sheet = book_copy.get_sheet(0)
    frecuencies = self.get_frecuencias()
    row = 1
    for f in frecuencies:
        sheet.write(row, 1, str(f[0]))
        row += 1
    book_copy.save('Examen.xls')

Tags: 文件selfgetexamplexlsexcelwritesheet