1

I have a .xlsm file as a reference template. I want to update the values of this .xlsm file using python from a .csv file.

template .xlsm ----> Update values using .csv

What has not worked : I tried using pandas.to_excel method. but the .xlsm file gets corrupted after I write to sheet.

Could someone please point me in the right direction ?

2 Answers 2

2

openpyxl supports xlsm file.

from openpyxl import load_workbook
wb2 = load_workbook('test.xlsm', keep_vba=True)
update(wb2, csvfile.csv) # this is where you need to work according to your need.
wb.save('new_document.xlsm')
wb.close()

https://openpyxl.readthedocs.io/en/default/tutorial.html

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, would give it a try
The code is badly written, and it won't work even if it is changed. Since the wb2.save('test.xlsm') will save it as a plain xls, without any xlsm functionalities.
Thanks. I will look into it.
Actually this worked for me, and the file was saved successfully, thank you
0

Maybe to try xlwings, using it something like this?

def update(workbook, csv_file):
    ws = workbook.sheets[2]
    ws.range('B14').value = 155

from xlwings import Book
wb = Book(r'test.xlsm')
update(wb, csv_file)
wb.save('test1.xlsm')
wb.close()

This is the best tool to update xlsm files since it uses WindowsAPI and macros are triggered in case something is changed. This means, it won't work on Linux.

Of course, update function must do something more meaningful than changing the B14 cell in the 3rd sheet.

For more info, please read http://docs.xlwings.org/en/stable/quickstart.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.