简体   繁体   English

使用df.to_excel将按钮插入电子表格

[英]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. 我正在尝试将按钮插入电子表格中,但无法正确使用insert_button。

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... ) 但是我无法在电子表格“ Info”中使用insert_button(可能是因为我尝试使用错误的方式...)

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 = workbook.add_worksheet()”时,我无法将数据框插入到工作表中,尝试时出现以下错误:

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. 因此,将数据框内容插入使用“ workbook.add_worksheet()”创建的工作表中,或将“ insert_button”插入由df.to_excel创建的电子表格中都可以解决此问题。

Thanks in advance 提前致谢

Try to use xlsxwriter to open directly the file, not pandas wrapper 尝试使用xlsxwriter直接打开文件,而不是pandas包装器

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. 这是Pandas和XlsxWriter的工作示例。 See also Working with Python Pandas and XlsxWriter in the XlsxWriter docs. 另请参阅XlsxWriter文档中的使用Python Pandas和XlsxWriter

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: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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