简体   繁体   English

如何使用xlrd包处理xlsx文件读取错误(“断言tvalue不是None”)?

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

Below Python code is to read xls & xlsx file. Python代码下面是读取xls和xlsx文件。 Im facing a problem while reading an xlsx file. 我在阅读xlsx文件时遇到问题。 Error is "assert tvalue is not None" 错误是“断言tvalue不是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: do_row中的/home/perlzuser/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/xlrd/xlsx.pyc(self,row_elem)723 else:724 bad_child_tag(child_tag) - > 725断言tvalue不是None 726 self.sheet.put_cell(rowx,colx,XL_CELL_TEXT,tvalue,xf_index)727否则:

AssertionError: Asse田:

How to resolve this issue in python? 如何在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. 这里我使用load_workbook读取文件并将该文件保存为file_new.xlsx。
  • I'm just copying the actual xlsx file(template.xlsx) data into a new xlsx file(file_new.xlsx) 我只是将实际的xlsx文件(template.xlsx)数据复制到一个新的xlsx文件中(file_new.xlsx)

Note: This copied file_new.xlsx can only be read by python code. 注意:这个复制的file_new.xlsx只能由python代码读取。 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. 您可以使用这些数据存储到DB或写入其他文件。

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

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