使用openpyxl编辑Excel2007文件(.xlsx)而不更改其自身样式吗? [英] Use openpyxl to edit a Excel2007 file (.xlsx) without changing its own styles?

查看:170
本文介绍了使用openpyxl编辑Excel2007文件(.xlsx)而不更改其自身样式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.xlsx文件要编辑,我发现 openpyxl 可以操纵Excel 2007文件. 我只想更改某些单元格中的值,而保持其他设置不变.

I have a .xlsx file to edit, I found openpyxl could manipulate Excel 2007 files. I only want to change the value in some cells and leave other settings unchanged.

但是在浏览文档之后,我找不到用于编辑现有文件的示例.它只是演示了读取.xlsx文件并写入新文件.

But after I went through the Documentation, I cannot find the examples to edit a existing file. It only demostrated reading a .xlsx file and writing to a new one.

我尝试了以下方法来编辑现有文件,但是在保存后,文件中的样式已被删除(例如字体,颜色):

I tried below way to edit an existing file, but after I saved it, the styles in the file has been removed( like fonts, colors):

from openpyxl.reader.excel import load_workbook
wb=load_workbook(r'd:\foo1.xlsx')
ws=wb.get_sheet_by_name('Bar')
ws.cell('A1').value= 'new_value'
# save the workbook to a new file to finish the editing
# but the style settings has been removed (such like font, color) in the new file
wb.save(r'd:\foo2.xlsx')

推荐答案

现在,openpyxl无法处理足够好的样式,因此我尝试使用pywin32 COM并获得了解决方案. 这是一个很好的 python-excel-mini-cookbook ,可将pywin32 COM用于Excel

Now openpyxl cannot handle styles good enough, so I tried using pywin32 COM and got the solution. Here is a good python-excel-mini-cookbook to use pywin32 COM for Excel

这篇关于使用openpyxl编辑Excel2007文件(.xlsx)而不更改其自身样式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆