简体   繁体   English

Python Openpyxl out of memory 在读写操作期间

[英]Python Openpyxl out of memory during read write operation

I have a large xl file(45 MB: 15 sheets, each with exactly 7 columns and 65536 rows).我有一个很大的 xl 文件(45 MB:15 张,每张正好有 7 列和 65536 行)。 I am trying to append all the data from these 15 sheets into 1 sheet on a separate workbook.我正在尝试将这 15 张表格中的所有数据 append 放入单独的工作簿上的一张表格中。 I'm using read_only=True for the base xl sheet and write_only=True for the Workbook to be written to.我将 read_only=True 用于基本 xl 工作表,将 write_only=True 用于要写入的工作簿。

The issue I am facing is that the after the code is done processing with 11 sheets(check code below), it throws an out of memory error:我面临的问题是,在用 11 张纸处理完代码后(检查下面的代码),它会抛出 memory 错误:

Done with sheet[ 9 ] Done with sheet[ 10 ] MemoryError完成工作表 [9] 完成工作表 [10] MemoryError

Using the code below:使用下面的代码:

import openpyxl as xl
wb1 = xl.load_workbook(path,read_only=True)
wb2 = xl.Workbook(write_only=True)
wb2_ws1=wb2.create_sheet()
sheets = wb1.sheetnames

print("Combine function read only")
for j in range(len(sheets)):

    for row in wb1[sheets[j]].iter_rows(min_row=2 if j != 0 else 1,max_row=wb1[sheets[j]].max_row, min_col=None, max_col=None,values_only=False):
        wb2_ws1.append(cell.value for cell in row)
    print("Done with sheet[",j,"]")

wb2.save(dest_filename)

Any suggestions on how to make this work?关于如何使这项工作的任何建议?

Do you have lxml installed?你有安装 lxml 吗? – Charlie Clark 57 mins ago – 查理克拉克 57 分钟前

@CharlieClark - installing lxml worked. @CharlieClark - 安装 lxml 有效。 – Vishwesh Chaubal just now Edit – Vishwesh Chaubal 刚刚编辑

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM