0

I'm trying to update an excel .xlsm with macros. Which library is best for this? I'm using at the moment Openpyxl, but it doesn't work properly. The following lines work, but the saved excel looses a bit of format (borders and I'm afraid it will loose macro functionality too).

from openpyxl import load_workbook
path = "my_path.xlsm"
# Load workbook
wb = load_workbook(path, keep_vba=True)
sheet_names = wb.sheetnames
# wb.get_sheet_names()
print(sheet_names)
# Select sheet
ws = wb["Sheet1"]
# Some Operations in excel
# Save the file
wb.save("my_path_new.xlsm")

Any other library recommendations?

Many thanks, Carmen

  • You could try using openpyxl just to find the cells of interest, and then win32com (assuming you run your script on a Windows machine) to make the necessary changes and save. See https://kyleellefsen.com/blog/2017_10_28_python_excel/ for win32com usage examples. – BoboDarph Oct 26 '18 at 09:58
  • Many thanks Bobo! Let's hope that works! :) – Carmen González-Conde Máiz Oct 26 '18 at 10:22
  • I'm having trouble using Win32. I'm having two different errors using what you send and some other code with win32 Error 1: File "C:\Users\cwx623167\AppData\Local\Continuum\anaconda3\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__ raise AttributeError("%s.%s" % (self._username_, attr)) AttributeError: Excel.Application.Workbooks error: "This COM object can not automate the makepy process - please run makepy manually for this object") TypeError: This COM object can not automate the makepy process - please run makepy manually for this object – Carmen González-Conde Máiz Oct 26 '18 at 10:37
  • 2
    Sounds like you're referring to the formatting of merged cells see the documentation for how to handle those. Also, xlwings is probably better than win32com for working through Excel. – Charlie Clark Oct 26 '18 at 10:40
  • I've been trying to use xlwings ! It works perfect! Many thanks Charlie! :D – Carmen González-Conde Máiz Oct 26 '18 at 11:17

0 Answers0