简体   繁体   中英

If I am using pandas .to_excel, how do I assign a worksheet to a variable?

I have written an Excel sheet like so

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='sheet_name', header=True, index=True)

Now I want to add a text box to the worksheet. How do I assign the sheet to a variable to add a text box?

You can do it like this:

import pandas as pd


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

writer = pd.ExcelWriter('pandas_textbox.xlsx', engine='xlsxwriter')

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

workbook  = writer.book
worksheet = writer.sheets['Sheet1']

worksheet.insert_textbox(2, 3, "Hello world")

writer.save()

Output :

在此处输入图片说明

See Working with Python Pandas and XlsxWriter .

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