[970] Combine multiple Excel files into one Excel file with multiple sheets
You can combine multiple Excel files into one Excel file with multiple sheets using the Pandas library in Python. Here's a general approach:
- Read each Excel file into a Pandas DataFrame.
- Create an Excel writer object using Pandas.
- Write each DataFrame to a separate sheet in the Excel file.
Here's a code example demonstrating this process:
import pandas as pd # List of Excel file names to combine excel_files = ['file1.xlsx', 'file2.xlsx', 'file3.xlsx'] # Create a Pandas Excel writer object with pd.ExcelWriter('combined_file.xlsx') as writer: # Iterate over each Excel file for file in excel_files: # Read the Excel file into a DataFrame df = pd.read_excel(file) # Extract the file name (without extension) to use as the sheet name sheet_name = file.split('.')[0] # Write the DataFrame to the Excel file with the sheet name df.to_excel(writer, sheet_name=sheet_name, index=False)
In this example:
excel_files
is a list containing the names of the Excel files you want to combine.pd.ExcelWriter('combined_file.xlsx')
creates a Pandas Excel writer object that will write to a file named 'combined_file.xlsx'.- Inside the loop, each Excel file is read into a DataFrame using
pd.read_excel()
. - The sheet name for each DataFrame is extracted from the file name (without the extension) using
split('.')
. - Finally, each DataFrame is written to the Excel file using
df.to_excel()
with the appropriate sheet name. Theindex=False
parameter is used to prevent the DataFrame index from being written as a column in the Excel sheet.
This will result in a single Excel file named 'combined_file.xlsx' containing multiple sheets, each corresponding to one of the input Excel files.
分类:
Python Study
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-02-12 【525】GeoServer制作heatmap相关说明
2020-02-12 【459】淘宝添加客服等问题
2019-02-12 【371】Twitter 分类相关
2015-02-12 【159】解决由于google被封导致GoAgent不能使用的办法