0

I am trying to load a large .xlsx file using OpenPyXL, while loading a 80 MB .xlsx file my entire 8 GB of memory is getting full

enter image description here

Excel file has 4 sheets with 800 000 rows.

from openpyxl import load_workbook
wb = load_workbook('Record.xlsx')

def process(ws):
    '''
    Read all rows of a worksheet
    '''
    data = []
    for a, c, f, k in zip(ws['A'], ws['C'], ws['F'], ws['K']):
        data.append([a, c, f, k])

    return data

ws1 = wb.worksheets[0] # Sheet 1
data1 = process(ws1)
ws2 = wb.worksheets[1] # Sheet 2
data2 = process(ws2)
ws3 = wb.worksheets[2] # Sheet 3
data3 = process(ws3)
ws4 = wb.worksheets[3] # Sheet 4
data4 = process(ws4)

Why while loading 80 MB of excel file 8 GB of memory is not enough?

6
  • Can you show us your code, it looks like something is causing a memory leak maybe ? Commented Apr 20, 2018 at 11:57
  • is it really that you have only these 2 rows in your code? may be parsing later does give you the issue? I never had problems reading; and for parsing use generators(yield) Commented Apr 20, 2018 at 11:57
  • 1
    Have you tried read_only = True to use read only mode? Commented Apr 20, 2018 at 11:57
  • Possible duplicate of Fastest Way To Run Through 50k Lines of Excel File in OpenPYXL Commented Apr 20, 2018 at 12:08
  • @BcK I have updated the code Commented Apr 20, 2018 at 12:45

1 Answer 1

2

Try using the read_only = True argument to use read only mode, as described here.

Sign up to request clarification or add additional context in comments.

4 Comments

While setting read_only = True, I am getting this error AttributeError: 'ReadOnlyWorksheet' object has no attribute 'iter_cols'
@User Read-only mode doesn't support iterating through columns, only rows. There's a suggested way to do it here.
I used for row in ws.rows to access the rows and it worked. Thank you
Glad I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.