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

 ̄綄美尐妖づ 提交于 2019-11-27 14:43:41

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

Styles aren't fully supported yet in openpyxl. Make sure you're using the latest version (1.5.3 as of now), as it recently improved its style capabilities.

The xlwt has more complete formatting for .xls files, but doesn't support xlsx as of yet.

Try the following code creating an instance of the following class:

from openpyxl import load_workbook

Class to manage excel data with openpyxl

class Copy_excel:
def __init__(self,src):
    self.wb = load_workbook(src)
    self.ws = self.wb.get_sheet_by_name("Sheet1")
    self.dest="destination.xlsx"

#Write the value in the cell defined by row_dest+column_dest         
def write_workbook(self,row_dest,column_dest,value):
    c = self.ws.cell(row = row_dest, column = column_dest)
    c.value = value

#Save excel file
def save_excel(self) :  
    self.wb.save(self.dest)

Never give up!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!