[Python] Access excel with macros using openpyxl

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have an .xlsm file with lots of macros and sheets, which provides some graphs when filled in. I want to open the .xlsm file in python and fill some values to the macros in order to get the graphs . I have tried the following code but it seems that when I open the new file a message that the file is corrupted pops-up.

    I use python 2.74 and openpyxl version is 2.4.8

    import openpyxl
    from xlwt import easyxf

    book = openpyxl.load_workbook('macroexcel.xlsm',keep_vba=True) #opens excel file
    sheet = book.get_sheet_by_name('SheetName') #Get the sheet name
    sheet['K34'] = 'Value' #Assign value to a cell connected to the macro
    book.save('newexcel.xlsm') #Save


    Is there a way to get access to the values in the macros, select them and extract the graphs using python.?
    Thanks.

    Continue reading...

Compartilhe esta Página