[英]How do I create a new excel sheet dynamically using python?
I want to create a excel sheet which doesnt exist till now dynamically on runtime. 我想创建一个excel表格,该表格在运行时动态地至今不存在。 I dont want to add sheets in excel sheet but I want to create a whole new excel file with the name of a random unique id created at runtime. 我不想在Excel工作表中添加工作表,但我想创建一个全新的Excel文件,其名称是在运行时创建的随机唯一ID。 Is there any chance I can do that using openpyxl or any other library in python. 我有没有可能使用openpyxl或python中的任何其他库来做到这一点。 Your any help will be appreciated. 您的任何帮助将不胜感激。 Thank you 谢谢
Absolutely! 绝对!
First you need the required libraries. 首先,您需要所需的库。 Personally, I've use the following combination libraries - and in my experience, they work exactly as expected. 就个人而言,我使用以下组合库-根据我的经验,它们完全可以按预期运行。
pip install pandas
熊猫 : pip install pandas
pip install XlsxWriter
XlsxWriter : pip install XlsxWriter
pip install xlrd
Xlrd (可选-用于读取Excel文件): pip install xlrd
Once you have them installed, you can adapt the following code snippet ( taken from XlsxWriter's docs ) to your requirements. 安装它们后,您可以根据需要修改以下代码片段( 摘自XlsxWriter的docs )。
import pandas as pd
# Create a Pandas dataframe from the 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')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.