首页 > 其他分享 >[971] [Keep original formats] Combine multiple Excel files into one Excel file with multiple sheets

[971] [Keep original formats] Combine multiple Excel files into one Excel file with multiple sheets

时间:2024-02-12 14:22:17浏览次数:24  
标签:files multiple Excel existing file path workbook sheet

If the existing Excel file has special formatting that prevents reading it directly with Pandas, you can use a library like openpyxl to handle the appending of new sheets. Here's how you can achieve this:

import os
from openpyxl import load_workbook

# Path to the existing Excel file
existing_excel_file = 'existing_file.xlsx'

# Path to the folder containing the Excel files to append
folder_path = 'folder_with_excel_files/'

# Load the existing Excel workbook
existing_wb = load_workbook(existing_excel_file)

# Iterate through the Excel files in the folder
for file_name in os.listdir(folder_path):
    if file_name.endswith('.xlsx'):
        # Construct the full path to the Excel file
        file_path = os.path.join(folder_path, file_name)
        
        # Load the Excel workbook to be appended
        append_wb = load_workbook(file_path)
        
        # Iterate through sheets in the workbook to be appended
        for sheet in append_wb.sheetnames:
            # Copy each sheet from the workbook to be appended to the existing workbook
            append_sheet = append_wb[sheet]
            existing_wb.create_sheet(title=sheet)
            existing_sheet = existing_wb[sheet]
            for row in append_sheet.iter_rows():
                for cell in row:
                    existing_sheet[cell.coordinate].value = cell.value

# Save the updated existing Excel workbook
existing_wb.save(existing_excel_file)

In this code:

  • existing_excel_file is the path to the existing Excel file.
  • folder_path is the path to the folder containing the Excel files you want to append.
  • We load the existing Excel workbook using openpyxl.load_workbook().
  • We iterate through the Excel files in the specified folder, load each workbook to be appended, and then iterate through its sheets.
  • For each sheet in the workbook to be appended, we create a corresponding sheet in the existing workbook, then copy the values from each cell in the sheet to the corresponding cell in the existing workbook.
  • Finally, we save the updated existing workbook.

标签:files,multiple,Excel,existing,file,path,workbook,sheet
From: https://www.cnblogs.com/alex-bn-lee/p/18013902

相关文章

  • 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、选择要分割的......
  • python爬虫爬取豆瓣电影top250并写入Excel中
    importrequestsimportreimportopenpyxl#创建工作表wb=openpyxl.Workbook()ws=wb.active#调整列距forletterin['B','C']:ws.column_dimensions[letter].width=66#发送网络请求headers={"User-Agent":'Mozilla/5.0(WindowsNT10.0;Win64;x64)......