Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

python - Use openpyxl to edit a Excel2007 file (.xlsx) without changing its own styles?

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.

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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...