使用 Python、ExcelWriter 和 openpyxl 创建一个空白工作表并向该新工作表添加文本
Creating a blank worksheet and adding text to that new worksheet using Python, ExcelWriter and openpyxl
我正在使用 ExcelWriter 和 openpyxl 引擎创建一个新的 excel 工作簿。我能够导出 作为副作用 创建新工作表的数据框,但是当我尝试添加
- 一个新的空白工作簿我收到错误 ("workbook is not defined")
- 文本到现有工作簿中的单元格时出现错误(“'Summary_Data' 未定义”)
我如何将下面的代码修改为
a) 添加一个新的空白工作表
b) 将文本添加到特定工作表中的特定单元格
我可能对尝试混合和匹配来自不同地方的各种代码片段感到困惑。
#export various dataframes to excel
with pd.ExcelWriter(f"data validation for {FNum}_{FName}.xlsx",engine='openpyxl') as writer:
DWoldfiltered.to_excel(writer,sheet_name="Previous_load")
#An attempt to create a new blank worksheet which fails
#worksheet1 = workbook.add_worksheet('Summary_Data')
#this works as a way of creating a worksheet
new_summary.to_excel(writer,sheet_name="Summary_Data",startrow=1,startcol=1)
old_summary.to_excel(writer,sheet_name="Summary_Data",startrow=13,startcol=1)
#two different attempts to a) reference the new workbook b) add text to specific cells,
#which both fail
Summary_Data.cell(0,1).value="Latest load summary"
Summary_Data.write(0,0,"Latest load summary")
writer.save()
使用writer.book
访问底层openpyxl工作簿对象,然后book.create_sheet()
初始化一个新的openpyxl工作表对象:
import pandas as pd
import pandas.util.testing as tm
# Create example dataframe
df = tm.makeDataFrame()
with pd.ExcelWriter("out.xlsx", engine='openpyxl') as writer:
# Write dataframe to excel worksheet
df.to_excel(writer, sheet_name="dataframe")
# Create new empty worksheet
# Note: `sheet` is a `openpyxl.worksheet.worksheet.Worksheet` object
book = writer.book
sheet = book.create_sheet("summary")
# Write data to worksheet cells
sheet.cell(1, 1).value = "Amount of dataframe rows:"
sheet.cell(2, 1).value = df.shape[0]
writer.save()
我正在使用 ExcelWriter 和 openpyxl 引擎创建一个新的 excel 工作簿。我能够导出 作为副作用 创建新工作表的数据框,但是当我尝试添加
- 一个新的空白工作簿我收到错误 ("workbook is not defined")
- 文本到现有工作簿中的单元格时出现错误(“'Summary_Data' 未定义”)
我如何将下面的代码修改为 a) 添加一个新的空白工作表 b) 将文本添加到特定工作表中的特定单元格
我可能对尝试混合和匹配来自不同地方的各种代码片段感到困惑。
#export various dataframes to excel
with pd.ExcelWriter(f"data validation for {FNum}_{FName}.xlsx",engine='openpyxl') as writer:
DWoldfiltered.to_excel(writer,sheet_name="Previous_load")
#An attempt to create a new blank worksheet which fails
#worksheet1 = workbook.add_worksheet('Summary_Data')
#this works as a way of creating a worksheet
new_summary.to_excel(writer,sheet_name="Summary_Data",startrow=1,startcol=1)
old_summary.to_excel(writer,sheet_name="Summary_Data",startrow=13,startcol=1)
#two different attempts to a) reference the new workbook b) add text to specific cells,
#which both fail
Summary_Data.cell(0,1).value="Latest load summary"
Summary_Data.write(0,0,"Latest load summary")
writer.save()
使用writer.book
访问底层openpyxl工作簿对象,然后book.create_sheet()
初始化一个新的openpyxl工作表对象:
import pandas as pd
import pandas.util.testing as tm
# Create example dataframe
df = tm.makeDataFrame()
with pd.ExcelWriter("out.xlsx", engine='openpyxl') as writer:
# Write dataframe to excel worksheet
df.to_excel(writer, sheet_name="dataframe")
# Create new empty worksheet
# Note: `sheet` is a `openpyxl.worksheet.worksheet.Worksheet` object
book = writer.book
sheet = book.create_sheet("summary")
# Write data to worksheet cells
sheet.cell(1, 1).value = "Amount of dataframe rows:"
sheet.cell(2, 1).value = df.shape[0]
writer.save()