Python Pandas:输出到 excel ".xls" spreadsheet 只是覆盖选定的 sheet

Python Pandas: Output to excel ".xls" spreadsheet with just overwriting the selected sheet

我有两个'.xls'格式的传播sheet,都有两个sheets(sheet1和sheet2)。我正在尝试用 book1.xls 中的 'sheet2' 覆盖 book2.xls 中的 'sheet2'。我正在尝试 pandas 方法,这是我的代码草稿,

import pandas as pd

# Open the first spreadsheet
df1 = pd.read_excel('Book1.xls', sheet_name='sheet2')

# Specify the writer file
writer = pd.ExcelWriter('Book2.xls')

# Write output
df1.to_excel(writer, 'sheet2', engine='xlswriter', index=False)

writer.save()

合并后,我希望只更新book2.xls中的'sheet2',同时保留'sheet1'。然而,情况并非如此,book2.xls 现在只有 'sheet2' 并删除了 'sheet1'。我查看了几个答案 (),它们使用 openpyxl 适用于“.xlsx”格式。我正在寻找处理“.xls”格式的解决方案。感谢您的帮助。

此外,我本来希望 pandas.ExcelWrite 有一个额外的选项来合并指定的 sheet,唉!

******************************

我尝试的另一种方法是这里,但是在合并 sheet.

时出现错误
import pandas as pd

# Open the first spreadsheet
df1 = pd.read_excel('Book1.xls')

excelBook = pd.ExcelFile(path+'Book2.xls')
writer = pd.ExcelWriter(path+'Book2.xls')
writer.book = excelBook

writer.sheets = dict((ws.title, ws) for ws in excelBook.sheet_names)

# Add new sheets
df1.to_excel(writer, "Sheet2", index=False)

# Save the file
writer.save()

`AttributeError: 'ExcelFile' object has no attribute 'add_sheet'`
import pandas as pd

# Open the BOTH spreadsheets
book1sheet1 = pd.read_excel('Book1.xls', sheet_name='sheet2')
book2sheet1 = pd.read_excel('Book2.xls', sheet_name='sheet1')

# Specify the writer file
writer = pd.ExcelWriter('Book2.xls')

# Write output
book2sheet1.to_excel(writer, 'sheet1', engine='xlswriter', index=False)
book1sheet1.to_excel(writer, 'sheet2', engine='xlswriter', index=False)

writer.save()

我相信这会奏效。当您 运行 to_excel 命令时,您将在开头导入并重写 Book2 Sheet 1。


为了保持工作表不加载到内存中,我会尝试这样的事情:

import pandas as pd
from openpyxl import load_workbook

path = "Book2.xls"

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

# this is the original sheet you wanted to add
df_added = pd.read_excel('Book1.xls', sheet_name='sheet2')


df_added.to_excel(writer, sheet_name = 'Book1Sheet1')

writer.save()
writer.close()

这是一种有效的解决方案。尽管 sheet 被合并,但与列关联的格式和链接没有合并。如果大家有更好的解决方案,欢迎分享。谢谢!

# Read excel file and get dict of sheetname(s), dataframe(s))

# File1
dict_1 = pd.read_excel('Book1.xls', sheet_name=None)

# File2
dict_2 = pd.read_excel('Book2.xls', sheet_name=None)

# Change the worksheet as dataframe
select_df1 = dict_1['Sheet2']

# Assign the selected dataframe to second worksheet
dict_2['Sheet2'] = select_df1

# Write all the sheets from dataframe2 that also has the updated sheet from File1
with pd.ExcelWriter('Book2.xls', 
                    engine='xlwt') as writer:
    # Write Sheets from dataframe2
    for ws_name, df_sheet in dict_2.items():
        df_sheet.to_excel(writer, sheet_name=ws_name, index=0)

writer.save()
writer.close()