首页 > 解决方案 > 合并两个带有多个工作表的excel文件而不会丢失格式

问题描述

我想分别使用python将多个 excel 文件与多个工作表合并。我不想丢失工作表中的任何格式。它应该复制所有工作表并只创建一个 Excel 文件。

我只能合并第一张纸,而且所有格式都丢失了。

这是我的代码:

import os
import os.path
import xlrd
import xlsxwriter

file_name = input("merge")
merged_file_name = file_name + ".xls"
dest_book = xlsxwriter.Workbook('m.xls')
dest_sheet_1 = dest_book.add_worksheet()
dest_row = 1
temp = 0
path = input("C:\\test")
out = os.path.isdir("")
print(out)

print("File path: " + path)
for root,dirs, files in os.walk("C:\\test"):
    for xlsfile in files:
        print ("File in mentioned folder is: " + xlsfile)
        temp_book = xlrd.open_workbook(os.path.join(root,xlsfile))
        temp_sheet = temp_book.sheet_by_index(0)
        if temp == 0:
            for col_index in range(temp_sheet.ncols):
                str = temp_sheet.cell_value(0, col_index)
                dest_sheet_1.write(0, col_index, str)
            temp = temp + 1
        for row_index in range(1, temp_sheet.nrows):
            for col_index in range(temp_sheet.ncols):
                str = temp_sheet.cell_value(row_index, col_index)
                dest_sheet_1.write(dest_row, col_index, str)
            dest_row = dest_row + 1
dest_book.close()
book = xlrd.open_workbook("m.xls")
sheet = book.sheet_by_index(0)
print ("number of rows in destination file are: "), sheet.nrows
print ("number of columns in destination file are: "), sheet.ncols

标签: pythonexcelpandasxlrdxlsxwriter

解决方案


由于您需要 Excel 的特定需求(如格式化),因此请考虑使用 COM 接口直接连接到 Excel 对象库。当然,这假设您在机器上安装了 Excel。对于 Windows,Python 可以使用该win32com库运行 COM,这不仅可以连接 Excel,还可以连接到大多数 Windows 应用程序和对象,包括记事本、Paint 甚至 ADODB。

本质上,这使用Workbooks.AddSheets.AddRange.Copy和其他方法镜像 VBA(它与 Excel 对象库具有类似的接口) 。所有其他 API,例如xlrd并且xlwriter不直接使用 Excel 方法,因此您甚至会丢失图形而不是数据的格式。

import os
import win32com.client as win32

path = input("C:\\test")
file_name = input("merge")
merged_file_name = file_name + ".xlsx"

try:
    # INITIALIZE EXCEL COM APP
    xlapp = win32.gencache.EnsureDispatch('Excel.Application')

    # ASSIGN CONSTANTS   
    xlPasteValues = -4163; lPasteFormats = -4122; xlWorkbookDefault = 51

    # CREATE NEW WOKRBOOK (PROMPTS IF EXISTS)
    new_wb = xlapp.Workbooks.Add()
    new_wb.SaveAs(Filename='MasterMerge.xlsx', FileFormat=xlWorkbookDefault)

    # LOOP THROUGH WORKBOOKS
    xl_files = [f for f in os.listdir(path) if f.endswith('.xls') or f.endswith('.xlsx')]

    for wb in xl_files:
        xlwb = xlapp.Workbooks.Open(os.path.join(path, wb))

        # LOOP THROUGH EVERY WORKSHEET, COPYING TO NEW WORKSHEET
        for xlsh in xlwb.Worksheets:
            new_sh = new_wb.Worksheets.Add()
            new_sh.Name = xlsh.Name
            new_wb.Save()            
            new_sh.Move(After=new_wb.Worksheets(new_wb.Worksheets.Count))

            xlsh.Cells.Copy(new_sh.Cells)
            new_sh = None

        xlwb.Close(False)
        xlwb = None

    # REMOVNIG DEFAULT SHEET AND LAUNCHING TO SCREEN
    new_wb.Worksheets('Sheet1').Delete()
    new_wb.Save()
    xlapp.Visible = True

except Exception as e:
    print(e)

finally:
    # RELEASE RESOURCES
    xlsh = None; new_sh = None; 
    xlwb = None; new_wb = None; xlapp = None

推荐阅读