加快openpyxl阅读速度

时间:2016-05-26 20:28:29

标签: python openpyxl

我正在开发一个非常大的程序,它正在重新获取数据,这是一个非常大的Excel电子表格。 由于一些奇怪的原因,加载这个大的excel文件需要很长时间,我希望以某种方式加速它。 我做了我自己的研究并尝试了read_only,但这并没有让我阅读任何细胞(至少如下所示)。任何建议都会受到欢迎。

def LoadSheet(Filename,worksheet_nr):
    import time as t
    st = t.clock()
    wb = xl.load_workbook(str(Filename))#workbook, loading workbook
    Sheets = wb.get_sheet_names()#Worksheets, shows all sheets in the workbook loaded
    ActiveSheet = wb.get_sheet_by_name(Sheets[worksheet_nr])#Selects the first sheet
    print round(t.clock() - st,3), "seconds of loading time."
    print "Sheet is loaded and reading..."
    return ActiveSheet



def CountRC_nr(ActiveSheet, StartR,StartC):
    R = StartR#Selected starting Row number
    C = StartC#Selected starting Column number
    Val = None #Value in cell (Right now, none is selected, thus zero value)
    while (Val != "None"):
        Val = str(ActiveSheet.cell(row=R,column=C).value)
        R += 1

    NumberRows = R - StartR - 1
    Val = None
    R=StartR

    while (Val != "None"):
        Val = str(ActiveSheet.cell(row=R,column=C).value)
        C += 1
    NumberColumns = C - StartC - 1

    return NumberRows, NumberColumns

由于你们中的一些人要求查看我如何使用read_only,这里是修改后的代码:

def LoadSheet(Filename,worksheet_nr):
    import time as t
    st = t.clock()
    wb = xl.load_workbook(str(Filename), read_only = True)#workbook, loading workbook
    Sheets = wb.get_sheet_names()#Worksheets, shows all sheets in the workbook loaded
    ActiveSheet = wb.get_sheet_by_name(Sheets[worksheet_nr])#Selects the first sheet
    print round(t.clock() - st,3), "seconds of loading time."
    print "Sheet is loaded and reading..."
    return ActiveSheet



def CountRC_nr(ActiveSheet, StartR,StartC):
    R = StartR#Selected starting Row number
    C = StartC#Selected starting Column number
    Val = None #Value in cell (Right now, none is selected, thus zero value)
    while (Val != "None"):
        Val = str(ActiveSheet.cell(row=R,column=C).value)
        R += 1

    NumberRows = R - StartR - 1
    Val = None
    R=StartR

    while (Val != "None"):
        Val = str(ActiveSheet.cell(row=R,column=C).value)
        C += 1
    NumberColumns = C - StartC - 1

    return NumberRows, NumberColumns

1 个答案:

答案 0 :(得分:1)

正如我已经说过的那样,你正在反对API。如果工作表的维度已知,那么您可以访问它们,否则您可以构建自己的计数器,没有什么意义。

对于单个查找来说,只读模式非常快,但由于每次查找都涉及将行中的所有单元格解析为感兴趣的单元格,因此对于大量单元格而言效率非常低,例如代码正在使用。

wb = load_workbook(filename, read_only=True)
for ws in wb:
    print(ws.dimensions)
    for row in ws.rows:
         for cell in row:
             print(cell.value)  # do whatever you want with the cell

您可以使用(cell.value for cell in row)轻松地将行转换为NumPy数组,但Pandas库也会将工作表直接读入数据框。