Add dataframe and button to same sheet with XlsxWriter

家住魔仙堡 提交于 2021-02-08 07:29:35

问题


I am able to create an excel file with in one sheet the data from a data frame and in a second sheet a button to run a macro What I need is to have both the data from the dataframe than the button in the same sheet

This is the code I found that I have tried to modify:

import pandas as pd
import xlsxwriter

df = pd.DataFrame({'Data': [10, 20, 30, 40]})

writer = pd.ExcelWriter('hellot.xlsx', engine='xlsxwriter')
worksheet = workbook.add_worksheet()
#df.to_excel(writer, sheet_name='Sheet1')

workbook  = writer.book
workbook.filename = 'test.xlsm'
worksheet = workbook.add_worksheet()
workbook.add_vba_project('./vbaProject.bin')

worksheet.write('A3', 'Press the button to say hello.')

#Add a button tied to a macro in the VBA project.
worksheet.insert_button('A1', {'macro':   'start',
                              'caption': 'Press Me',
                              'width':   80,
                              'height':  30})

df.to_excel(writer, sheet_name ='Sheet2') 

writer.save()


workbook.close()

回答1:


I know that you simply asked how to insert the button in the same sheet but i decided to check how the macros are working with xlsxwriter, so i wrote a complete tutorial on how to add a macro.

1) Firstly we need to create manually a file which will contain the macro in order to extract it as a bin file and inject it later using xlsxwriter. So we create a new excel file, go to Developer tab, Visual Basic, Insert Module and write the following code:

Sub TestMsgBox()
    MsgBox "Hello World!"
End Sub

Save the file with xlsm extension to contain the macro, e.g. as Book1.xlsm.

2) Now we need to extract the bin file. Open your cmd and browse to the directory where you have saved the Book1.xlsm. Then browse through file explorer to the folder where you have installed python (or to the virtual environment folder) and search for vba_extract.py. Copy this script into the same folder as the Book1.xlsm. Then type in cmd:

python vba_extract.py Book1.xlsm

This way you will extract the macro and create the vbaProject.bin file in the same folder.

3) Now it's time to create the final file. Delete the Book1.xlsm and the vba_extract.py files as they 're not needed anymore and run the following code:

import pandas as pd

# Create a test dataframe
df = pd.DataFrame({'Data': [10, 20, 30, 40]})

# Import it through the xlsxwriter
writer = pd.ExcelWriter('hello_world.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Create the workbook and the worksheet
workbook = writer.book
workbook.filename = 'hello_world.xlsm' # rename the workbook to xlsm extension
worksheet = writer.sheets['Sheet1']

# Inject the bin file we extracted earlier
workbook.add_vba_project('./vbaProject.bin')

# Insert a description
worksheet.write('B1', 'Press the button to say hello.')

#Add a button tied to a macro in the VBA project.
worksheet.insert_button('B2', {'macro': 'TestMsgBox',
                              'caption': 'Press Me',
                              'width': 80, 'height':  30})

# Finally write the file
writer.save()

Now button is in the same sheet as your data and working:



来源:https://stackoverflow.com/questions/60627303/add-dataframe-and-button-to-same-sheet-with-xlsxwriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!