alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[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:

  1. Read each Excel file into a Pandas DataFrame.
  2. Create an Excel writer object using Pandas.
  3. 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. The index=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.

posted on   McDelfino  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 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不能使用的办法
点击右上角即可分享
微信分享提示