有没有更快的方法在python中使用openpyxl将数据写入Excel工作表?

时间:2018-01-02 01:26:20

标签: python excel openpyxl

我目前正在编写一个程序来更新数据并将数据从一个电子表格复制到另一个电子表格。我写的代码工作得很好,但它实用起来需要太长时间。总的来说,执行此任务大约需要一个小时。电子表格也非常大我可能会添加,一个是20,000行乘30列,另一个是3,000行乘30列。代码更新较大电子表格中的特定行,然后将数据从较小的电子表格复制到较大的电子表格中(如果该数据尚不存在)。在分析了问题所在之后,我发现将数据复制并写入更大的电子表格花费了大部分时间(约55分钟)。 openpyxl中的write_only选项不支持根据需要写入现有文件,因此我不知道如何加快编写过程。我也是python的新手,所以任何帮助都会受到赞赏,谢谢!

这是代码:

# iterate through ticket column of first sheet
for roww in range (2, sheet.max_row+1):
    sheet1_ticket_number = sheet.cell(row=roww, column = 3).value
    # iterate through ticket column of second sheet
    # Ticket number x from sheet 1 compared to all ticket numbers in sheet 2
    for row2 in range(starting_row, (sheet2.max_row+1+sheet.max_row)):
        sheet2_ticket_number = sheet.cell(row = row2, column = 3).value

        # If ticket number matches, check to see if columns match, if not, update
        if (sheet.cell(row=roww, column = 3).value == sheet2.cell(row = row2, column = 3).value):
            check = 'true'
            for i in range(1, sheet.max_column+1):
                if sheet2.cell(row=row2, column = 3+i).value != sheet.cell(row=roww, column =3+i).value and (3+i != 15) and (3+i != 38) and (3+i != 14):
                    sheet2.cell(row=row2, column = 3+i).value = sheet.cell(row=roww, column =3+i).value
                    #print('updated row# ', row2, 'Column#', 3+i, 'ticket#', sheet2.cell(row=row2, column = 3).value,  'to:', sheet2.cell(row=row2, column = 3+i).value)

                if sheet2.cell(row=row2, column = 1).value is None:
                    sheet2.cell(row=row2, column = 1).value = sheet.cell(row=roww, column =1).value
                if sheet2.cell(row=row2, column = 2).value is None:
                    sheet2.cell(row=row2, column = 1).value = sheet.cell(row=roww, column =1).value
            break


        # if ticket number is not in second file/ empty row, add new ticket row w column entries. 
        if (sheet2.cell(row = row2, column = 3).value is None) and (sheet2.cell(row = row2+1, column = 3).value is None):
            sheet2.cell(row=row2, column =3).value = sheet1_ticket_number
            #print('printed new ticket row# ', sheet2.cell(row=row2, column =3).value)
            for j in range(1, sheet.max_column+1):
                if sheet2.cell(row=row2, column = 3+j).value != sheet.cell(row=roww, column =3+j).value:
                    sheet2.cell(row=row2, column = 2).value = sheet.cell(row=roww, column =2).value
                    sheet2.cell(row=row2, column = 1).value = sheet.cell(row=roww, column =1).value
                    sheet2.cell(row=row2, column = 3+j).value = sheet.cell(row=roww, column =3+j).value
            break       

0 个答案:

没有答案