[英]How to copy and paste data from each excel sheet into a final sheet using Python?
我在一個工作簿中有 7 個 Excel 工作表,我正在嘗試將每個 Excel 工作表中的數據復制並粘貼到我的最終工作表中。 下面的代碼創建了名為“Final Sheet”的最終工作表,但不會復制每張工作表中的任何數據。 我需要一個循環來遍歷每個工作表並將數據復制並粘貼到最終工作表中,但不知道該怎么做。
表 1 = 北美,表 2 = 日本,表 3 = 中國等
`#create final list sheet
open = openpyxl.load_workbook(filepath)
ws2 = open.create_sheet('Final List') # this creates the final sheet
open.save(filepath)`
`#put data into final list
wb = openpyxl.load_workbook(filepath)
sheet1 = open.get_sheet_by_name('North America')
finalListSheet = open.get_sheet_by_name('Final List')
wb.save(filepath)`
這里問了一個類似的問題: Python Loop through Excel sheet, place into one df
我在這里簡化一下。 此方法使用 Pandas:
import pandas as pd
sheets_dict = pd.read_excel(filepath, sheetname=None)
full_table = pd.DataFrame()
//Loop in sheets
for name, sheet in sheets_dict.items():
sheet['sheet'] = name
full_table = full_table.append(sheet)
//Need to save the DF in your Final Sheet
這是關於如何在特定 Excel 工作表中保存數據框(DF) 的另一個問題: Pandas Dataframe to excel sheet
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.