简体   繁体   中英

how to handle xlsx file reading error (“assert tvalue is not None”) using xlrd package?

Below Python code is to read xls & xlsx file. Im facing a problem while reading an xlsx file. Error is "assert tvalue is not None"

--> workbook=xlrd.open_workbook("/home/perlzuser/sprint12/template.xlsx")
sheet=workbook.sheet_by_index(0)    
data=[[sheet.cell_value(r,c) for c in range(sheet.ncols)]for r in range (sheet.nrows)]
print " ::",data

My error:::

/home/perlzuser/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/xlrd/xlsx.pyc in do_row(self, row_elem) 723 else: 724 bad_child_tag(child_tag) --> 725 assert tvalue is not None 726 self.sheet.put_cell(rowx, colx, XL_CELL_TEXT, tvalue, xf_index) 727 else:

AssertionError:

How to resolve this issue in python?

I have found a work around to resolve assertion error:

code:

import xlrd
from openpyxl import load_workbook

wb = load_workbook("/home/perlzuser/sprint12/template.xlsx")
wb.save("file_new.xlsx")
workbook=xlrd.open_workbook("file_name.xlsx")

sheet=workbook.sheet_by_index(0)

data=[[sheet.cell_value(r,c) for c in range(sheet.ncols)]for r in range (sheet.nrows)]

print "output:", data

Code Explanation:

  • Here I'm using load_workbook to read the file and saving that file as file_new.xlsx.
  • I'm just copying the actual xlsx file(template.xlsx) data into a new xlsx file(file_new.xlsx)

Note: This copied file_new.xlsx can only be read by python code. It will be not in human readable format but your code will read it. You can use those data to store into DB or to write into other files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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