VB macro is lost in openpyxl 2.4.0

Issue #705 resolved
Hugh Foster created an issue

The following code fails to keep the VBA macro when opening and writing data to an excel 2010 xlsm file.

    w_book = load_workbook(
        filename=os.path.join(out_dir, file_dir_name + ".xlsm"),
        keep_vba=True)

    # Add data to worksheet here   

    w_book.save(os.path.join(out_dir, file_name + ".xlsm"))

This works fine however in 2.3.5.

I've attached a test script and macro file. Note that you will have to change 2 path variables at the top of the script.

Also I'm using Python 3.4.3.

Comments (16)

  1. CharlieC

    Thanks. Well, the good news is that the macros are essentially preserved. Must be yet another issue with Excel's packaging...

  2. Monique Bakker

    Thanks for the resolve @charlie_x but how do I make this permanent? Every time I restart Spyder I have to re-run your code to make sure macros won't be lost.

  3. Hugh Foster reporter

    Further to this, the VBA macro is now preserved but some of the formatting is lost. Specifically the currency format in the pivot table is lost.

    This works in openpyxl 2.2.1 but seems to be broken in 2.3.5 and isn't working with this change for 2.4.1. Should I open a new item?

  4. Log in to comment