简体   繁体   English

如何使用 python Z3A43B4F88325D2FA5AZC22 在特定单元格中获取 dataframe 以覆盖现有 excel?

[英]How can i get a dataframe to overwrite an existing excel in a specific cell using python pandas?

I have written the below which doesnt seem to be working.我已经写了下面这似乎不起作用。 I am defining dataframe as d3, to be a columnn with 20 rows with the comment "my comment".我将 dataframe 定义为 d3,它是一个有 20 行并带有“我的评论”注释的列。 I am then trying to add that into an existing saved down excel doc in cell L2.然后,我尝试将其添加到单元格 L2 中现有的已保存 excel 文档中。 The dataframe is printing and looks right in the IDE, but just doesnt overwrite in the excel. dataframe 正在打印,在 IDE 中看起来正确,但在 excel 中没有覆盖。 any help would be greatly appreciated, as im just starting out with python!任何帮助将不胜感激,因为我刚开始使用 python!

from openpyxl import load_workbook
import pandas as pd
import xlsxwriter

#create dataframe to be 19 lines with comment "my comment"
comment = "my comment"
df3 = pd.DataFrame([comment]* 20)
print(df3)
wb = load_workbook(r'H:\myfile.xlsx')

writer = pd.ExcelWriter(r'H:\myfile', engine='xlsxwriter')
df3.to_excel(writer,sheet_name='mysheet', startrow = 2, startcol = 12)
wb.save(r'H:\myfile.xlsx')
print("done")

As far as I can tell, you are doing multiple things at once which negate each other's effect.据我所知,您同时做多项事情,这会抵消彼此的影响。 Why are youn even using openpyxl?为什么你甚至使用openpyxl?

You load the workbook from the excel file with openpyxl and load_workbook , then you override you excel file with the pandas DataFrame, at which point the output should be the one you want. You load the workbook from the excel file with openpyxl and load_workbook , then you override you excel file with the pandas DataFrame, at which point the output should be the one you want. But after that, you again override the excel file with the content of the loaded workbook which you loaded before the changes you made with pandas, which restores the excel file to its original state. But after that, you again override the excel file with the content of the loaded workbook which you loaded before the changes you made with pandas, which restores the excel file to its original state.

Get rid of every line involving openpyxl and it should work.摆脱涉及 openpyxl 的每一行,它应该可以工作。

import pandas as pd
import xlsxwriter

#create dataframe to be 19 lines with comment "my comment"
comment = "my comment"
df3 = pd.DataFrame([comment]* 20)
print(df3)

writer = pd.ExcelWriter(r'H:\myfile', engine='xlsxwriter')
df3.to_excel(writer,sheet_name='mysheet', startrow = 2, startcol = 12)
print("done")

If you want to modify your excel file with pandas, you don't need openpyxl, if you want to modify you excel with openpyxl, you don't need pandas. If you want to modify your excel file with pandas, you don't need openpyxl, if you want to modify you excel with openpyxl, you don't need pandas.

from the docs: "note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased"来自文档:“请注意,使用已存在的文件名创建 ExcelWriter object 将导致现有文件的内容被删除”

so yeah, you need openpyxl or create all that you want to see in the file inside python.所以是的,您需要 openpyxl 或创建您想在 python 内的文件中看到的所有内容。

暂无
暂无

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

相关问题 如何使用 Python 将 Pandas DataFrame 导出到 Google 表格(特定单元格)? - How can I Export Pandas DataFrame to Google Sheets (specific cell) using Python? 如何使用Pandas将python Web抓取数据导出到现有excel文件中的特定工作表? - How can I export my python web scrape data to a specific sheet in an existing excel file using pandas? 如何使用 Pandas 在多表 Excel 工作簿中用新数据框覆盖现有工作表? - How to overwrite existing worksheet with new dataframe in a multisheet Excel workbook using Pandas? append pandas dataframe 到现有的 excel 文件,从特定单元格开始 - append pandas dataframe to existing excel file with start at specific cell 如何从 Python Pandas 中的 Excel 文件打印 dataframe 的单元格名称? - How can I print the cell name of a dataframe from an Excel file in Python Pandas? 如何使用 Python 中的 Pandas 从 csv 获得特定值? - How can I get a specific valuefrom the csv with using Pandas in Python? 如何覆盖 pandas DataFrame 中的数组单元格? - How to overwrite an array cell in pandas DataFrame? 如何使用 pandas 从 excel 文件中获取单元格的背景颜色? - How can I get the background color of a cell from an excel file using pandas? 如何从 excel 中获取 pandas dataframe 以显示数据类型? - how can i get pandas dataframe from excel in showing datatype? 如何使用 python 在 excel 中获取特定列/单元格的水平值 - How do I get the value of a specific column / cell going horizontal in an excel using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM