Home » excel » python – xlrd cannot read xlsx file downloaded from email attachment

python – xlrd cannot read xlsx file downloaded from email attachment

Posted by: admin March 9, 2020 Leave a comment

Questions:

This is a very very strange issue. I have quite a large excel file (the contents of which I cannot discuss as it is sensitive data) that is a .xlsx and IS a valid excel file.

When I download it from my email and save it on my desktop and try to open the workbook using xlrd, xlrd throws an AssertionError and does not show me what went wrong.

When I open the file using my file browser, then save it (without making any changes), it works perfectly with xlrd.

Has anyone faced this issue before? I tried passing in various flags to the open_workbook function to no avail and I tried googling for the error. So far I haven’t found anything.

The method I used was as follows

    file = open('bigexcelfile.xlsx')
    fileString = file.read()
    wb = open_workbook(file_contents=filestring)

Please help! The error is as follows

    Traceback (most recent call last):
      File "./varify/samples/resources.py", line 354, in post
        workbook = xlrd.open_workbook(file_contents=fileString)
      File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/__init__.py", line 416, in open_workbook
        ragged_rows=ragged_rows,
      File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 791, in open_workbook_2007_xml
        x12sheet.process_stream(zflo, heading)
      File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 528, in own_process_stream
        self_do_row(elem)
      File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 722, in do_row
        assert tvalue is not None
    AssertionError

How to&Answers:

rename or Save as your Excel file as .xls instead of .xlsx
Thank You

Answer:

Use pyopenxl, not xlrd, for this format: https://openpyxl.readthedocs.org/en/latest/