技术文摘
Python 批量合并同一文件夹内子文件夹 Excel 文件所有 Sheet 数据的四种方法
2024-12-31 03:54:46 小编
Python 批量合并同一文件夹内子文件夹 Excel 文件所有 Sheet 数据的四种方法
在数据处理工作中,我们经常会遇到需要将同一文件夹内子文件夹中的多个 Excel 文件的所有 Sheet 数据进行合并的情况。使用 Python 可以高效地实现这一需求,以下介绍四种常见的方法。
方法一:使用 pandas 库
pandas 是 Python 中强大的数据处理库。遍历文件夹和子文件夹获取所有 Excel 文件路径,然后依次读取每个文件的所有 Sheet 数据,使用 concat 函数进行合并。
import pandas as pd
import os
def merge_excels_pandas(folder_path):
file_paths = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.xlsx'):
file_paths.append(os.path.join(root, file))
data_frames = []
for file_path in file_paths:
excel_file = pd.ExcelFile(file_path)
for sheet_name in excel_file.sheet_names:
data_frames.append(pd.read_excel(file_path, sheet_name=sheet_name))
merged_data = pd.concat(data_frames)
return merged_data
方法二:结合 openpyxl 库
openpyxl 库专门用于处理 Excel 文件。通过类似的文件路径遍历,读取每个 Sheet 的数据并合并。
import openpyxl
import os
def merge_excels_openpyxl(folder_path):
data = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.xlsx'):
file_path = os.path.join(root, file)
workbook = openpyxl.load_workbook(file_path)
for sheet_name in workbook.sheetnames:
sheet = workbook[sheet_name]
rows = sheet.rows
sheet_data = [[cell.value for cell in row] for row in rows]
data.extend(sheet_data)
return data
方法三:利用 xlrd 库
xlrd 也是处理 Excel 的常用库之一。
import xlrd
import os
def merge_excels_xlrd(folder_path):
data = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.xls') or file.endswith('.xlsx'):
file_path = os.path.join(root, file)
workbook = xlrd.open_workbook(file_path)
for sheet_index in range(workbook.nsheets):
sheet = workbook.sheet_by_index(sheet_index)
rows = sheet.get_rows()
sheet_data = [[cell.value for cell in row] for row in rows]
data.extend(sheet_data)
return data
方法四:使用 win32com 库(适用于 Windows 系统)
对于 Windows 环境,win32com 库可以提供更直接的 Excel 操作。
import win32com.client
import os
def merge_excels_win32com(folder_path):
excel_app = win32com.client.Dispatch("Excel.Application")
data = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.xlsx'):
file_path = os.path.join(root, file)
workbook = excel_app.Workbooks.Open(file_path)
for sheet in workbook.Sheets:
rows = sheet.UsedRange.Rows
sheet_data = [[cell.Value for cell in row] for row in rows]
data.extend(sheet_data)
workbook.Close(False)
excel_app.Quit()
return data
以上四种方法各有特点,可以根据实际需求和环境选择合适的方法来批量合并 Excel 文件的 Sheet 数据,提高数据处理的效率。
- Golang 虚拟币充值中身份验证与余额更新的实现方式
- 在带 sandbox 属性的 iframe 里运用 Selenium 的方法
- Python代码中反复调用f1.readlines()导致list index out of range错误的原因
- 解析字符串层级结构问题:怎样区分字符串中不同层级分隔符
- Python3中index()函数查找列表元素索引的方法
- 用一个Channel控制多个Goroutine顺序执行hello world的方法
- 通过GitLab CI/CD与Terraform实现Lambda用于SFTP集成及Go中的S Databricks
- CrawlSpider中Rule解析过的链接如何进行定制化处理
- Python函数异常处理:自定义函数执行正常调用后却只输出一条消息问题的解决方法
- Python代码中print(list(g))后为何无法再执行print(i)
- 微信支付成功后怎样实现页面跳转
- BARK - Textdio模型全新呈现
- Go语言循环中顶格单词Label的含义
- Go中time.Now().Format("2006.01.02") 为何格式化为2006年1月2日
- Python报错无法解析JSON数据的解决方法