我有一个包含多个选项卡的 xlsx 文件,其中一个是
Town_names
,其中已经包含一些数据。
我想用数据框覆盖该数据 -
Town_namesDF
- 同时保持其余 xlsx 选项卡完好无损。
我尝试过以下方法:
with pd.ExcelWriter(r'path/to/file.xlsx', engine='openpyxl', mode='a') as writer:
Town_namesDF.to_excel(writer,sheet_name='Town_names')
writer.save()
writer.close()
但它最终会创建一个新选项卡
Town_names1
,而不是覆盖Town_names
选项卡。我错过了什么吗?谢谢。
因为你想覆盖,但没有直接的选项(就像在 julia 的 XLSX 中,有 cell_ref 的选项)。只需删除重复项(如果存在),然后写入即可。
with pd.ExcelWriter('/path/to/file.xlsx',engine = "openpyxl", mode='a') as writer:
workBook = writer.book
try:
workBook.remove(workBook['Town_names'])
except:
print("worksheet doesn't exist")
finally:
df.to_excel(writer, sheet_name='Town_names')
writer.save()
从 pandas 1.3.0 版本开始。有一个新参数:“if_sheet_exists” {‘错误’、‘新’、‘替换’}
pd.ExcelWriter(r'path/to/file.xlsx', engine='openpyxl', mode='a', if_sheet_exists='replace')
您可以尝试暂时存储所有其他工作表,然后将它们添加回来。我不认为这会保存任何公式或格式。
Store_sheet1=pd.read_excel('path/to/file.xlsx',sheetname='Sheet1')
Store_sheet2=pd.read_excel('path/to/file.xlsx',sheetname='Sheet2')
Store_sheet3=pd.read_excel('path/to/file.xlsx',sheetname='Sheet3')
with pd.ExcelWriter(r'path/to/file.xlsx', engine='openpyxl', mode='a') as writer:
Town_namesDF.to_excel(writer,sheet_name='Town_names')
Store_sheet1.to_excel(writer,sheet_name='Sheet1')
Store_sheet2.to_excel(writer,sheet_name='Sheet2')
Store_sheet3.to_excel(writer,sheet_name='Sheet3')
writer.save()
writer.close()
嗯,我已经成功做到了。这不是一个干净的解决方案,而且一点也不快,但我使用了 openpyxl 文档来处理此处找到的 pandas:https://openpyxl.readthedocs.io/en/latest/pandas.html
我有效地选择了
Town_names
工作表,使用 ws.delete_rows()
清除它,然后将数据帧的每一行附加到工作表中。
wb = openpyxl.load_workbook(r'path/to/file.xlsx')
ws = wb.get_sheet_by_name('Town_names')
ws.delete_rows(0, 1000)
wb.save(r'path/to/file.xlsx')
wb = openpyxl.load_workbook(r'path/to/file.xlsx')
activeSheet = wb.get_sheet_by_name('Town_names')
for r in dataframe_to_rows(Town_namesDF, index=False, header=True):
activeSheet.append(r)
for cell in activeSheet['A'] + activeSheet[1]:
cell.style = 'Pandas'
wb.save(r'path/to/file.xlsx')
有点混乱,我希望有比我更好的解决方案,但这对我有用。
您可以使用 xlwings 来完成该任务。 xlwings 的要求是安装 Microsoft Excel。这是一个例子:
import xlwings as xw
import pandas as pd
path = r"test.xlsx"
df = pd._testing.make_DataFrame()
# The with block inserts df to an existing Excel worksheet,
# in this case to the one with the name "Town_names".
with xw.App(visible=False):
wb = xw.Book(path)
ws = wb.sheets["Town_names"]
ws.clear()
ws["A1"].value = df
# If formatting of column names and index is needed as xlsxwriter does it, the following lines will do it.
ws["A1"].expand("right").api.Font.Bold = True
ws["A1"].expand("down").api.Font.Bold = True
ws["A1"].expand("right").api.Borders.Weight = 2
ws["A1"].expand("down").api.Borders.Weight = 2
wb.save(path)
wb.close()