简体   繁体   English

如何将多个 XLSX 文件合并到一个工作簿中,同时保持它们的工作表分开?

[英]How to merge multiple XLSX files into one Workbook while keeping their worksheets separate?

I'm trying to merge all.xlsx files within a folder into one workbook while keeping their worksheets separated.我正在尝试将文件夹中的所有.xlsx 文件合并到一个工作簿中,同时保持它们的工作表分开。 My code below is grabbing specified files, merging them into one workbook but only copying the first worksheet from each file instead of all their worksheets.我下面的代码是抓取指定的文件,将它们合并到一个工作簿中,但只复制每个文件中的第一个工作表,而不是所有工作表。

My output file is currently one workbook containing the first worksheet of each merged file but some files have multiple worksheets.我的 output 文件目前是一个工作簿,其中包含每个合并文件的第一个工作表,但有些文件有多个工作表。 Would anyone know how I can grab all worksheets from multiple files and combine them into one workbook?有谁知道我如何从多个文件中获取所有工作表并将它们合并到一个工作簿中?

Thanks for your time!谢谢你的时间!

import win32com.client as win32
import os

excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()

path = r'C:\Users\Desktop\merge'
files = os.listdir(path)

for f in [os.path.join(os.getcwd(), "Daily Pipeline"), os.path.join(os.getcwd(), "Loans")]: 
    w = excel.Workbooks.Open(f) 
    w.Sheets(1).Copy(wb.Sheets(1))


wb.SaveAs(os.path.join(os.getcwd(), "Final.xlsx"))
excel.Application.Quit()

Why dont you try reading the excel files using python pandas?为什么不尝试使用 python pandas 读取 excel 文件? You could use glob to read the files from your directory and then merge them into a dataframe.您可以使用 glob 从目录中读取文件,然后将它们合并到 dataframe 中。

import glob

all_data = pd.DataFrame()
path = 'd:/projects/chassis/data/*.xlsx'
for f in glob.glob(path):
    df = pd.read_excel(f, sheet_name=None, ignore_index=True, skiprows=6, usecols=8)
    cdf = pd.concat(df.values())
    all_data = all_data.append(cdf,ignore_index=True)
print(all_data)

This might do the work!这可能会起作用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个xlsx文件合并到一个具有不同工作表的单个xlsx文件中 - How to merge multiple xlsx files into one single xlsx file with Different sheets 如何通过 openpyxl 将多个 xlsx 文件合并为一个具有不同工作表的 xlsx 文件 - How to merge multiple xlsx files into one single xlsx file with Different sheets by openpyxl 如何在一个数据集中合并多个Excel xlsm文件和多个工作表? - How do I merge multiple excel xlsm files, with multiple worksheets in one Dataset? 将多个 HTML 文件作为单独的工作表导入 Excel - Importing Multiple HTML Files Into Excel as Separate Worksheets 如何将多个 excel 工作簿组合成具有多个工作表的单个工作簿 - How to combine multi excel workbook into single workbook with multiple worksheets 如何在for循环中编写同一工作簿中的单独Excel工作表? - How to write to separate excel worksheets within the same workbook in a for loop? 如何将Excel工作簿中的所有工作表另存为一个文本文件? - How to save all worksheets in Excel workbook as one text file? python:如何使用熊猫读取同一工作簿的多个工作表 - python: How to read multiple worksheets of the same workbook using pandas 如何使用 Xlwings 和 Python 合并同一个 Excel 工作簿中的所有工作表 - How to Merge all worksheets within the same excel workbook using Xlwings & Python 如何将 3 列合并为 1 列同时保持值列分开 - How to merge 3 columns into 1 whilst keeping values column separate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM