Python實(shí)現(xiàn)合并多個Excel文件中的指定sheet
本文將介紹一個用于合并多個Excel文件中指定sheet的Python代碼。這個功能可以方便地整理和分析數(shù)據(jù)。我們將逐步解釋代碼的每個部分,并提供示例用法。
導(dǎo)入庫
首先,我們導(dǎo)入了需要使用的三個庫:os、pandas和time。這些庫分別用于操作文件和文件夾、處理Excel文件以及計(jì)算程序執(zhí)行時間。
import os import pandas as pd import time
定義函數(shù)
我們定義了一個名為merge_excel_sheets的函數(shù),用于將多個Excel文件中的指定sheet合并到一個新的Excel文件中。該函數(shù)接受三個參數(shù):folder_path(文件夾路徑)、excel_list(包含要合并的Excel文件和sheet名的列表)和output_file(輸出文件路徑)。
def merge_excel_sheets(folder_path, excel_list, output_file):
start_time = time.time()
with pd.ExcelWriter(output_file) as writer:
for excel_name, sheet_name in excel_list:
file_path = os.path.join(folder_path, excel_name)
df = pd.read_excel(file_path, sheet_name=sheet_name)
sheet_name_combined = f"{excel_name[:-5]}-{sheet_name}"
df.to_excel(writer, sheet_name=sheet_name_combined, index=False)
end_time = time.time()
execution_time = end_time - start_time
print(f"程序執(zhí)行時間:{execution_time}秒")在函數(shù)內(nèi)部,我們首先記錄程序開始執(zhí)行的時間。然后,我們使用pd.ExcelWriter創(chuàng)建一個空的Excel Writer對象,用于寫入合并后的數(shù)據(jù)。
start_time = time.time() with pd.ExcelWriter(output_file) as writer:
接下來,我們使用一個循環(huán)來處理每個Excel文件和sheet。對于每個文件和sheet,我們構(gòu)造完整的文件路徑,并使用pd.read_excel讀取數(shù)據(jù)并存儲為DataFrame對象。
for excel_name, sheet_name in excel_list:
file_path = os.path.join(folder_path, excel_name)
df = pd.read_excel(file_path, sheet_name=sheet_name)然后,我們構(gòu)造合并后的sheet名稱,格式為"原文件名-原sheet名",并使用df.to_excel將DataFrame對象中的數(shù)據(jù)寫入到指定的sheet中。
sheet_name_combined = f"{excel_name[:-5]}-{sheet_name}"
df.to_excel(writer, sheet_name=sheet_name_combined, index=False)最后,我們計(jì)算程序執(zhí)行的時間,并將其打印出來。
end_time = time.time()
execution_time = end_time - start_time
print(f"程序執(zhí)行時間:{execution_time}秒")示例用法
我們提供了一個示例用法,包括文件夾路徑、要合并的Excel文件和sheet的列表,以及輸出文件路徑。通過調(diào)用merge_excel_sheets函數(shù),我們可以執(zhí)行合并操作。
folder_path = "E:\\工作內(nèi)容"
excel_list = [
("一店9月.xlsx", "原始數(shù)據(jù)"),
("二店9月.xlsx", "原始"),
("三店9月.xlsx", "原始數(shù)據(jù)"),
("四店9月.xlsx", "原始數(shù)據(jù)"),
("五店9月-離職.xlsx", "原始數(shù)據(jù)")
]
output_file = os.path.join(folder_path, "output.xlsx")
merge_excel_sheets(folder_path, excel_list, output_file)完整代碼
import os
import pandas as pd # 導(dǎo)入pandas庫
import time # 導(dǎo)入時間庫,用于計(jì)算程序執(zhí)行時間
def merge_excel_sheets(folder_path, excel_list, output_file):
start_time = time.time() # 記錄程序開始執(zhí)行的時間
# 創(chuàng)建一個空的Excel Writer對象,用于寫入合并后的數(shù)據(jù)
with pd.ExcelWriter(output_file) as writer:
# 循環(huán)處理每個Excel文件和sheet
for excel_name, sheet_name in excel_list:
# 根據(jù)文件名和文件夾路徑,構(gòu)造完整的文件路徑
file_path = os.path.join(folder_path, excel_name)
# 讀取指定Excel文件中指定sheet的數(shù)據(jù),并存儲為DataFrame類型的對象
df = pd.read_excel(file_path, sheet_name=sheet_name)
# 構(gòu)造合并后的sheet名稱,格式為"原文件名-原sheet名"
sheet_name_combined = f"{excel_name[:-5]}-{sheet_name}"
# 將DataFrame對象中的數(shù)據(jù)寫入到指定sheet中
df.to_excel(writer, sheet_name=sheet_name_combined, index=False)
end_time = time.time() # 記錄程序結(jié)束執(zhí)行的時間
execution_time = end_time - start_time # 計(jì)算程序執(zhí)行的時間
print(f"程序執(zhí)行時間:{execution_time}秒") # 輸出程序執(zhí)行的時間
# 示例用法
folder_path = "E:\\工作內(nèi)容"
excel_list = [
("一店9月.xlsx", "原始數(shù)據(jù)"),
("二店9月.xlsx", "原始"),
("三店9月.xlsx", "原始數(shù)據(jù)"),
("四店9月.xlsx", "原始數(shù)據(jù)"),
("五店9月-離職.xlsx", "原始數(shù)據(jù)")
]
output_file = os.path.join(folder_path, "output.xlsx")
merge_excel_sheets(folder_path, excel_list, output_file) # 調(diào)用合并函數(shù),將指定的Excel文件中指定sheet的數(shù)據(jù)進(jìn)行合并到此這篇關(guān)于Python實(shí)現(xiàn)合并多個Excel文件中的指定sheet的文章就介紹到這了,更多相關(guān)Python合并Excel中指定sheet內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中如何將Tqdm與Asyncio結(jié)合使用呢
這篇文章主要和大家詳細(xì)介紹了在Python中如何將Tqdm與Asyncio結(jié)合使用呢,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
使用Python實(shí)現(xiàn)簡單的數(shù)據(jù)備份
數(shù)據(jù)備份,即數(shù)據(jù)的復(fù)制和存儲,是指將數(shù)據(jù)從一個位置復(fù)制到另一個位置,以防止原始數(shù)據(jù)丟失或損壞,下面我們就來了解一下用Python如何實(shí)現(xiàn)這一功能吧2025-03-03
使用python3批量下載rbsp數(shù)據(jù)的示例代碼
這篇文章主要介紹了使用python3批量下載rbsp數(shù)據(jù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
分析Python中設(shè)計(jì)模式之Decorator裝飾器模式的要點(diǎn)
這篇文章主要介紹了Python中設(shè)計(jì)模式之Decorator裝飾器模式模式,文中詳細(xì)地講解了裝飾對象的相關(guān)加鎖問題,需要的朋友可以參考下2016-03-03

