首页 > 其他分享 >[970] Combine multiple Excel files into one Excel file with multiple sheets

[970] Combine multiple Excel files into one Excel file with multiple sheets

时间:2024-02-12 14:22:36浏览次数:23  
标签:files multiple name Excel DataFrame file sheet

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.

标签:files,multiple,name,Excel,DataFrame,file,sheet
From: https://www.cnblogs.com/alex-bn-lee/p/18013901

相关文章

  • [971] [Keep original formats] Combine multiple Excel files into one Excel file w
    IftheexistingExcelfilehasspecialformattingthatpreventsreadingitdirectlywithPandas,youcanusealibrarylikeopenpyxltohandletheappendingofnewsheets.Here'showyoucanachievethis:importosfromopenpyxlimportload_workbook......
  • C#使用MiniExcel导入导出数据到Excel/CSV文件
    MiniExcel简介简单、高效避免OOM的.NET处理Excel查、写、填充数据工具。目前主流框架大多需要将数据全载入到内存方便操作,但这会导致内存消耗问题,MiniExcel尝试以Stream角度写底层算法逻辑,能让原本1000多MB占用降低到几MB,避免内存不够情况。特点:低内存耗用,避免OOM、频繁......
  • SharePoint Online 中Excel引用另一个Excel中的单元格
    前言最近,碰到一个需求,用户想要在SharePointOnline中从一个Excel文件引用另一个Excel文件的内容,所以就研究了一下。正文1.建一个测试的Excel文件,随便写点东西,如下图:2.新建另一个Excel,用来引用上一个Excel文件,如下图:3.在第一个Excel中的文件,选中单......
  • nginx里alias,root,try_files笔记
    先说结果:try_files一共有三个值$uri,$uri//index.html,前两个值取决于alias,最后一个值和alias无关,取决于root,即如果最后一个值/index.html,则实际地址是root/index.html,不是alias/index.html,和alias一点关系没有,还有root,alias可以说不是一个东西,root中文意思根路径,限定......
  • 第 4章 用 CSV 和 Excel 存储数据
    第4章用CSV和Excel存储数据4.1用CSV文件存储数据CSV(Comma-SeparatedValues)其实就是纯文本,用逗号分隔值,可以分隔成多个单元格。CSV文件除了可以用普通的文本编辑工具打开,还能用Excel打开,但CSV和Excel有以下不同:所有值都是字符串类型。不支持设置字体颜色和样......
  • 43、excel快速填充序列号,删除行时序号自动跟上
    平时填充序号的做法:首先在第1、2行输入1、2,然后用手往下拖动,填充后面的行,缺点:当我删除一行时,后面的序号不会自动按顺序填充上 解决方法:1、在excel上选中A6单元格,然后左上角输入A6:A110,按【回车】键2、直接输入【=ROW()-1】,再按【ctrl+回车】键盘就可以了缺点:由于公......
  • dremio FileSystem 简单说明
    dremio尽管对于文件系统的使用很多底层都是hdfs的(s3,发射加速),dremio为了减少直接依赖hdfs,自己抽象了一个FileSystem接口对于不同的实现可以方便进行扩展,当然和刚才说的一样,不少底层依赖的是hdfs的FileSystem参考子类如下图简单说明:FilterFileSystem实现了FileSy......
  • #排列组合#CF1550D Excellent Arrays
    洛谷传送门CF1550D分析对于excellent的\(a\)来说\(|a_i-i|=x\)的值是固定的,考虑枚举它一半正一半负时函数值是最大的,当\(n\)为奇数时要分为两种情况(不过可以通过杨辉三角合并)问题是,由于\(l,r\)的范围,并不能做到所有位置都能可正可负,不过不超过\(mn=\min\{1-l,r-n\}......
  • 【office编程】VB删除指定excel中行
    打开Excel中的VisualBasic编辑器,键入以下代码:SubBatchDelete()DiminputstrAsStringDimresult()AsStringDimsheet_no()AsStringDimstart_end()AsStringDimi,n,num,WS_CountAsIntegerDimwsAsWorksheetinputst......
  • [office] Excel怎么设置从指定部分分页打印
    excel表格中的指定一部分数据想分成两页打印,该怎么设置呢?下面就跟小编一起看看吧。Excel设置从指定部分分页打印的步骤1、现有如下字段:按班级进行打印。一班的同学打印在一张纸上。二班的同学打印在一张纸上!2、点击视图菜单下面的分页预览3、选择要分割的......