is there any way to create a .xlsm file from scratch in python?

后端 未结 3 880
感动是毒
感动是毒 2021-01-22 16:35

I am using python 3.8.1 on a mac and am trying to create a .xlsm file from scratch. I have looked at openpyxl and xlsxwriter, both of them are able to create

相关标签:
3条回答
  • 2021-01-22 17:05

    It appears as though an XLSM file is a zipped Excel file, containing macros, etc.

    I was able to find a potential fix here using openpxyl:

    import openpyxl as px
    from openpyxl import load_workbook
    import os
    ...
    wbname='orig_fname.xlsm'
    wb = load_workbook(filename=wbname, keep_vba=True)
    ...
    wb.save('temp.xlsm')
    os.rename('temp.xlsm', wbname)
    

    Please let me know if it works for you.

    0 讨论(0)
  • 2021-01-22 17:14

    Thanks to both Alexander Pushkarev and APhillips for helping out with this question. Going off of Alexander's post I was able to figure out a hack to get this to work. I'm not really proud of this, but it works.

    Running Alexander's code I get this error:

    Exception ignored in: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1819, in del self.close() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1836, in close self.fp.seek(self.start_dir) ValueError: I/O operation on closed file.

    I played around with this and found out that if I took out keep_vba=True from the load_workbook function the code ran but I still got the error I noted above when trying to open the .xlsm file with Excel.

    So, looking at the latest error I saw the last line says

    I/O operation on closed file.

    I looked at openpxyl documentation and tried opening the file without the keep_vba=True option before opening it with keep_vba=True and it worked.

    So excuse this ugly code, but this will work to create a .xlsm file from scratch without depending on any existing files (copy and paste ready):

    from openpyxl import Workbook
    from openpyxl import load_workbook
    
    wb = Workbook()
    ws = wb.active
    ws['A1'] = 42
    ws.append([1, 2, 3])
    wb.save('new_document.xlsm')
    wb1 = load_workbook('new_document.xlsm')
    wb2 = load_workbook('new_document.xlsm', keep_vba=True)
    wb2.save('new_document.xlsm')
    
    0 讨论(0)
  • 2021-01-22 17:16

    To the best of my knowledge, xlsm format is essentially the same as xlsx, with the only difference that it can contain macroses (see https://wiki.fileformat.com/spreadsheet/xlsm/ for instance).

    So, you can use openpyxl (https://openpyxl.readthedocs.io/en/stable/), and you don't need to do anything else other than saving your xlsx file with the xlsm extension (code is taken from the official doc and changed slightly):

    from openpyxl import Workbook
    wb = Workbook()
    ws = wb.active
    ws['A1'] = 42
    ws.append([1, 2, 3])
    wb.save('new_document.xlsm')
    

    However, there's a strange behaviour in openpyxl which is not documented properly, so to make file actually openable in the Excel you will also need:

    wb = load_workbook('new_document.xltm', keep_vba=True)
    wb.save('new_document.xlsm')
    

    There's a little bit of explanation for this here: https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-as-a-stream

    So the complete code:

    from openpyxl import Workbook
    wb = Workbook()
    ws = wb.active
    ws['A1'] = 42
    ws.append([1, 2, 3])
    wb.save('new_document.xlsm')
    # and the workaround
    wb = load_workbook('new_document.xltm', keep_vba=True)
    wb.save('new_document.xlsm')
    
    0 讨论(0)
提交回复
热议问题