简体   繁体   English

Python Excel 在现有工作表中写入 DataFrame

[英]Python Excel write a DataFrame in an existing sheet

I'd like to add a dataframe in an existing sheet (here "sheet1") but the code creates a new "sheet1".我想在现有工作表(此处为“sheet1”)中添加一个数据框,但代码创建了一个新的“sheet1”。 Here is my code:这是我的代码:

Thank you very much for your help非常感谢您的帮助

Alex亚历克斯

import pandas as pd
import numpy as np
from openpyxl import load_workbook

path = ""
df = pd.DataFrame({'Data': [11, 12, 13, 14]})
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine = 'openpyxl')
writer.book = book
df.to_excel(writer,sheet_name="Sheet1" startrow=1, startcol=5, header=False,index=False)
writer.save()
writer.close()

I was wondering if you just bring in the whole xl library?我想知道你是否只带了整个 xl 库? I haven't tried pandas yet, but it's on my project roadmap.我还没有尝试过熊猫,但它在我的项目路线图上。

    import openpyxl

You can try this:你可以试试这个:

from openpyxl import load_workbook

wb = load_workbook("C:\text.xlsx")
sheets = wb.sheetnames
Sheet1 = wb[sheets[8]]
Sheet2 = wb[sheets[7]]
#Then update as you want it
Sheet1 .cell(row = 2, column = 4).value = 5 #This will change the cell(2,4) to 4
wb.save("HERE PUT THE NEW EXCEL PATH") 

for more information : enter link description here欲了解更多信息: 在此处输入链接描述

暂无
暂无

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

相关问题 Python:将数据框写入到现有的Excel中,其中包含带有图像的工作表 - Python: Write a dataframe to an already existing excel which contains a sheet with images 打开现有 Excel 文件、清除现有工作表并将数据框写入该工作表的最佳方法 - Best way to open existing excel file, clear an existing sheet and write dataframe to that sheet 如何在现有 Excel 工作表下方写入数据框而不会丢失数据透视表中的 excel 切片器? - How to write dataframe below an existing Excel sheet without losing slicer of excel in pivot table sheet? "使用 python pandas 将现有的 excel 表附加到新的数据框" - Append existing excel sheet with new dataframe using python pandas 如何将 python dataframe 导出到现有的 excel 工作表并保留格式? - How to export python dataframe into existing excel sheet and retain formatting? Python pandas:将变量写入现有工作表中的 excel 单元格 - Python pandas: Write variable to excel cell in existing sheet 想要读取一个文件并使用 python 写入现有的 excel 表 - Want to read an a file and write to existing excel sheet using python 使用DataFrame python将控制台输出写入excel表 - write console output to excel sheet using DataFrame python 将 pandas dataframe 逐列写入现有的 excel 模板跳过 ZBF57C906FA7D25856D07372E 中的表格列 - Write pandas dataframe column by column to existing excel template skipping excel sheet columns that have formulas in it python:将数据框更新到现有的excel工作表,而不会覆盖同一工作表和其他工作表上的内容 - python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM