[英]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.