用python覆盖现有excel文件中的一张表

问题描述

我有一个名为 test.xlsx 的现有 Excel 文件,它在第一张 tables 中有一个表格,它指向第二张 data 中的数据。

我想保留 tables 表及其表格,同时用 data 数据框的内容覆盖整个 new_df 表。

我的程序:

import pandas as pd
import openpyxl
from openpyxl import load_workbook
file_to_update = r'C:\folder\test.xlsx'

book = load_workbook(file_to_update)
writer = pd.ExcelWriter(file_to_update,engine = 'openpyxl')
writer.book = book

new_df = pd.read_csv(r'C:\folder\other_csv.csv',sep = '|')
new_df.to_excel(writer,index = False)
writer.save()
writer.close()

先前存在的数据仍然存在,但新数据框 new_df 数据位于新工作表 Sheet1 中。

目标是保留现有的 tables 表,同时使用新数据框 new_df 中的数据覆盖数据表。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)