簡體   English   中英

Python:從 excel 工作簿復制工作表並粘貼到新工作簿

[英]Python: Copy sheets from excel workbook and paste into new workbook

我是一個超級初學者,還在學習 Python。

我有一個 excel 工作簿,其中包含多張工作表,只希望將某些工作表復制並粘貼到新創建的工作簿中,我遇到了一些麻煩。

下面是我的代碼。

import pandas as pd
import openpyxl

df = pd.read_excel('AMT.xlsb', sheet_name=['Roster','LOA'])

# print whole sheet data
with pd.ExcelWriter('output.xlsx') as writer:
    df.to_excel(writer, sheet_name=['Roster','LOA'])

我收到錯誤“IndexError:至少一張紙必須可見”,AMT 文件中的所有紙都沒有被隱藏。

看起來您可能正在將框架轉換為字典 - 試試這個:

import pandas as pd
import openpyxl

df = pd.read_excel('AMT.xlsb', sheet_name='Roster')
df1 = pd.read_excel('AMT.xlsb', sheet_name='LOA')


# print whole sheet data
with pd.ExcelWriter('output.xlsx') as writer:
    df.to_excel(writer, sheet_name="Roster", index=False)
    df1.to_excel(writer, sheet_name="LOA", index=False)

之后你可能還有一些清理......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM