简体   繁体   English

如何在不先导出数据框的情况下写入 Excel 工作表?

[英]How to write to an Excel sheet without exporting a dataframe first?

I am trying to write some text to a specific sheet in an Excel file.我正在尝试将一些文本写入 Excel 文件中的特定工作表。 I export a number of pandas dataframes to the other tabs, but in this one I need only some text - basically some comments explaining how the other tabs were calculated.我将一些 Pandas 数据框导出到其他选项卡,但在这个选项卡中,我只需要一些文本 - 基本上是一些解释其他选项卡是如何计算的注释。

I have tried this but it doesn't work:我试过这个,但它不起作用:

import pandas as pd
writer=pd.ExcelWriter('myfile.xlsx')
writer.sheets['mytab'].write(1,1,'This is a test')
writer.close()

I have tried adding writer.book.add_worksheet('mytab') and我曾尝试添加writer.book.add_worksheet('mytab')

ws=writer.sheets['mytab']
ws.write(1,1,'This is a test')

but in all cases I am getting: keyerror:'mytab' .但在所有情况下,我都得到: keyerror:'mytab' The only solution I have found is to write an empty dataframe to the tab before writing my text to the same tab:我找到的唯一解决方案是在将我的文本写入同一个选项卡之前将一个空数据框写入选项卡:

emptydf=pd.DataFrame()
emptydf['x']=[None]
emptydf.to_excel(writer,'mytab',header=False, index=False)

I could of course create a workbook instance, as in the example on the documentation of xlsxwriter: http://xlsxwriter.readthedocs.io/worksheet.html However, my problem is that I already have a pd.ExcelWriter instance, which is used in the rest of my code to create the other excel sheets.我当然可以创建一个工作簿实例,如 xlsxwriter 文档中的示例: http ://xlsxwriter.readthedocs.io/worksheet.html 但是,我的问题是我已经有一个 pd.ExcelWriter 实例,它被使用在我的其余代码中创建其他 excel 表。

I even tried passing a workbook instance to to_excel() , but it doesn't work:我什至尝试将工作簿实例传递给to_excel() ,但它不起作用:

workbook   = xlsxwriter.Workbook('filename.xlsx')
emptydf.to_excel(workbook,'mytab',header=False, index=False)

Is there any alternative to my solution of exporting an empty dataframe - which seems as unpythonic as it can get?我的导出空数据框的解决方案是否有任何替代方案 - 它看起来像它可以得到的那样非 Pythonic?

You mentioned that you used add_worksheet() method from the writer.book object, but it seems to work and do what you wanted it to do.您提到您使用了 writer.book 对象中的 add_worksheet() 方法,但它似乎可以工作并执行您想要的操作。 Below I've put in a reproducible example that worked successfully.下面我放了一个成功的可重现的例子。

import pandas as pd

print(pd.__version__)

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook  = writer.book
ws = workbook.add_worksheet('mytab')

ws.write(1,1,'This is a test')

writer.close()

Thought I'd also mention that I'm using pandas 0.18.1.以为我还会提到我正在使用熊猫 0.18.1。

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

相关问题 如何在现有 Excel 工作表下方写入数据框而不会丢失数据透视表中的 excel 切片器? - How to write dataframe below an existing Excel sheet without losing slicer of excel in pivot table sheet? 如何将 dataframe 导出到 excel 表并按类别分组 - How to exporting dataframe to excel sheet with grouping based in categories 熊猫和xlsxwriter:如何在不导出数据框的情况下创建新工作表? - Pandas and xlsxwriter: how to create a new sheet without exporting a dataframe? 在没有订阅的情况下使用 pandas 将 DataFrame 导出到 Excel - Exporting DataFrame to Excel using pandas without subscribe 如何在没有保护的情况下编写 Excel 工作表? - How do I write Excel sheet without protected it? 使用 Pandas 将 Dataframe 行写入 excel 表 - Write Dataframe row to excel sheet using Pandas Python Excel 在现有工作表中写入 DataFrame - Python Excel write a DataFrame in an existing sheet 如何使用 xlsxwriter 在没有 dataframe 的情况下写入 excel 文件 - how to write into excel file without dataframe using xlsxwriter 导出到Excel时如何冻结第一列 - How to freeze the first column when exporting to excel 如何分组并获取 Dataframe 中每个人的唯一值计数,然后将其写入 Python 中的 Excel 工作表? - How can I groupby and get unique value counts for each person in Dataframe and then write that to an Excel sheet in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM