excel - Python: How to open a multisheet .xslx file (with formatting) and edit a few cells and save it as another .xlsx file -
i have tried using openpyxl seems fail when saving file (http://pastebin.com/vu5ltajh) , cannot find info problem.
any other modules let read edit , save .xlsx file while retaining style , formatting of original file?
you can use win32com library (i assume you're working on windows) , dispatch excel application via com. inside, can use every method available in vba (so that's work object model reference documentation).
below simplified example how use it.
import win32com.client excel = win32com.client.gencache.ensuredispatch ("excel.application") excel.visible = true workbook = excel.workbooks.open(filepath) if isinstance(worksheetname, str): worksheet = workbook.worksheets(worksheetname) else: #assume worksheetname number sheet = workbook.worksheets[worksheetname] def getrange(worksheet, address): return worksheet.range(address) def getvalue(range_): """returns values specified range, tuple of tuples (even if range single row) or single value if range 1 cell""" return range_.value2 def setvalue(range_, values): range_.value2 = values
as see workbook object has method saveas
, i'd use save file after changing cells value, this:
workbook.saveas("newfilename")
Comments
Post a Comment