将工作表从工作簿复制到另一个工作簿,无需删除/更改现有值、公式、格式等以定位 Excel 工作簿

问题描述 投票:0回答:1

我基本上正在获取原始数据集,并将其导入到我经常使用的另一个工作簿中,目标工作簿中已存在公式/值/格式/列分组。我想将原始数据复制到现有工作表中另一个工作簿,而不更改目标电子表格中的任何其他内容。

我有基本的 Pandas 副本,并且了解这将复制电子表格,并删除所有其他值。
我认为我的逻辑可能是错误的,我需要获取我想要复制的数据集并将其转换为 Pandas 数据框,然后将其复制到目标列/行,也许这不会删除/更改中的现有值细胞?

import pandas as pd

data = pd.read_excel('/Users/mikey/Desktop/Automating Financial Reporting/QBO Spreadsheets/Statement of Cash Flows/StatementofCashFlows-ABC.xlsx', sheet_name="Month")

data.to_excel('/Users/mikey/Desktop/Automating Financial Reporting/QBO Spreadsheets/Statement of Cash Flows/StatementofCashFlows-ABCReporting.xlsx',sheet_name="ABC Cash Flows",startcol=17,startrow=9,index=False,header=None)
python pandas excel
1个回答
0
投票

这里使用的默认引擎是Xlsxwriter,它总是会创建一个新文件。

如果您想保留现有的工作表/数据,请使用 Openpyxl 作为附加模式('a')的引擎。

import pandas as pd
data = pd.read_excel('/Users/mikey/Desktop/Automating Financial Reporting/QBO Spreadsheets/Statement of Cash Flows/StatementofCashFlows-ABC.xlsx', sheet_name="Month")

output_excel_file = '/Users/mikey/Desktop/Automating Financial Reporting/QBO Spreadsheets/Statement of Cash Flows/StatementofCashFlows-ABCReporting.xlsx'


with pd.ExcelWriter(output_excel_file,mode='a', if_sheet_exists='overlay', engine='openpyxl') as writer:
    data.to_excel(writer,
                  sheet_name="ABC Cash Flows",
                  startcol=17,
                  startrow=9,
                  index=False,
                  header=False
    )
© www.soinside.com 2019 - 2024. All rights reserved.