简体   繁体   中英

Insert Button to Spreadsheet using df.to_excel

I'm trying to insert a button into a spreadsheet, but I'm not able to use insert_button properly.

What I did so far :

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
df_tst.to_excel(writer, sheet_name='Info' ,index = False , header = False) 
workbook.add_vba_project(r'C:\Users\...\Project.bin')
workbook.filename = 'test.xlsm'
writer.save()

But I'm not able to use insert_button to the spreasheet " Info " ( probably because I'm trying the wrong way... )

Then I tried a different option that so far it works as expected, but what I'm trying to do is to insert a button just like the following :

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
worksheet1 = workbook.add_worksheet()
worksheet1.write('A1', 'TEST.')
worksheet1.insert_button('C6', {'macro': 'macro_test',
                           'caption': 'Macro Test',
                           'width': 100,
                           'height': 80})
workbook.add_vba_project(r'C:\Users\...\Project.bin')
workbook.filename = 'test.xlsm'
writer.save()

The problem is that using " worksheet1 = workbook.add_worksheet() ", I'm not able to insert the dataframe to the sheet, giving the following error when I tried :

worksheet1.write(df_tst)

Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
worksheet1.write(df_tst)
File "C:\Users\...\worksheet.py", line 63, in cell_wrapper
int(first_arg)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DataFrame'

So either inserting the dataframe content into the worksheet created using "workbook.add_worksheet()" or using the "insert_button" to the spreadsheet created by df.to_excel would solve the problem.

Thanks in advance

Try to use xlsxwriter to open directly the file, not pandas wrapper

import xlsxwriter
workbook   = xlsxwriter.Workbook('test.xlsx')
worksheet1 = workbook.add_worksheet()
worksheet1.write('A1', 'TEST.')
worksheet1.insert_button('C6', {'macro': 'macro_test',
                       'caption': 'Macro Test',
                       'width': 100,
                       'height': 80})
workbook.add_vba_project(r'C:\Users\...\Project.bin')
workbook.filename = 'test.xlsm'
writer.save()

Here is a working example with Pandas and XlsxWriter. See also Working with Python Pandas and XlsxWriter in the XlsxWriter docs.

import os
import pandas as pd
import xlsxwriter

# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

worksheet.set_column('D:D', 30)

# Add the VBA project binary.
workbook.add_vba_project('./vbaProject.bin')

# Show text for the end user.
worksheet.write('D3', 'Press the button to say hello.')

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

# Close the Pandas Excel writer and output the Excel file.
writer.save()

# Pandas doesn't allow a '.xslm' extension but Excel requires
# it for files containing macros so we rename the file.
os.rename('pandas_simple.xlsx', 'pandas_simple.xlsm')

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM