简体   繁体   English

Python - 将工作表从多个工作簿复制到一个工作簿

[英]Python - Copy sheet from multiple workbooks to one workbook

Here is my process:这是我的过程:

Step 1: Open File1第 1 步:打开 File1
Step 2: Load Sheet1步骤 2:加载 Sheet1
Step 3: Load OutputFile步骤 3:加载输出文件
Step 4: Create a new sheet in OutputFile第 4 步:在 OutputFile 中创建一个新工作表
Step 5: Copy contents cell by cell from Step 2 to paste in the sheet created in Step 4第 5 步:从第 2 步中逐个单元格复制内容以粘贴到第 4 步中创建的工作表中
Step 6: Repeat the process 'n' number of times第 6 步:重复该过程“n”次

I have created a Python script to achieve this but the program is insanely slow.我已经创建了一个 Python 脚本来实现这一点,但该程序非常慢。 Takes an hour to complete.需要一个小时才能完成。 Here is a snippet of the code that does the copying over.这是进行复制的代码片段。

import xlsxwriter as xlsx
import openpyxl as xl

for i in range (6,k):
    #get the file location/name from source file
    filename = sheet.cell_value(i,3)
    #get the sheetname from the sheet read in the above statement
    sheetname = sheet.cell_value(i,4)
    #print the file name to verify
    print(filename)
    #get output sheet name
    outputsheetname = sheet.cell_value(i,5)

    #load the source workbook
    wb1 = xl.load_workbook(filename=filename,data_only = True)
    #get the index of sheet to be copied
    wb1_sheet_index = wb1.sheetnames.index(sheetname)
    #load the sheet
    ws1 = wb1.worksheets[wb1_sheet_index]

    #load the output workbook
    wb2 = xl.load_workbook(filename=output_loc)
    #create a new sheet in output workbook
    ws2 = wb2.create_sheet(outputsheetname)
    #print(ws2,":",outputsheetname)

    for row in ws1:
        for cell in row:
            ws2[cell.coordinate].value = cell.value

    wb2.save(output_loc)

wb2.save(output_loc)

The filename, sheetname and outputsheetname comes from a master excel sheet where I keep the file location and sheet names.文件名、工作表名和输出工作表名来自我保存文件位置和工作表名称的主 Excel 工作表。 I load this file before this loop.我在这个循环之前加载这个文件。

Also, I want the contents of the cell to be copied.另外,我希望复制单元格的内容。 If the source sheet has any formula, I do not want that to be copied over.如果源表有任何公式,我不希望它被复制。 And if there is a value 500 in Cell A5, I want the value to be in cell A5 in the output sheet.如果单元格 A5 中有一个值 500,我希望该值位于输出表中的单元格 A5 中。

Maybe I am approaching this the wrong way.也许我以错误的方式接近这个。 Any help is appreciated.任何帮助表示赞赏。

openpyxl is the slowest module to work with excel file. openpyxl 是处理 excel 文件最慢的模块。 you can try doing it with xlwings or if you're okay to use any excel add-in here is the RDB Merge that you can prefer using it, it is comparetively fast and does work您可以尝试使用 xlwings 来执行此操作,或者如果您可以使用任何 excel 加载项,这里是您可以更喜欢使用它的 RDB Merge ,它相对较快并且确实有效

暂无
暂无

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

相关问题 如何使用Python合并来自不同工作簿的多个同名excel工作表并保存到新的excel工作簿中 - How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 使用python将范围从多个工作簿复制到主工作簿中的新工作表 - Copy range from multiple workbooks to new worksheets in a master workbook using python Openpyxl Python 复制自一个 Excel 表格并粘贴到子文件夹中的现有个人工作簿中 - Openpyxl Python Copy From One Excel Sheet&Paste in Existing Individual Workbooks in Subfolders 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 将多个工作簿中的特定工作表连接到一个 df 时出错 - Error concatenating specific sheet from multiple workbooks into one df 使用宏将工作表复制到Python中的工作簿 - Copy sheet with Macros to workbook in Python 将工作簿中的多个 Excel 工作表合并为一张工作表 Python - Combine Multiple Excel sheets within Workbook into one sheet Python 如何使用 python 将 excel 工作簿中的内容复制到另一个工作簿而不丢失 excel 格式 - How to copy contents from a sheet of an excel workbook to another workbook without loosing the excel formatting using python 有没有办法在python中将Excel工作簿组合成一个主工作簿? - Is there a way to combine Excel workbooks into one Master workbook within python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM