第三方模块xlrd的一个异常

Workbook corruption: seen[2] == 4

使用xlrd读取excel文件时, 出现异常

xlrd.compdoc.CompDocError: Workbook corruption: seen[2] == 4

This error occurs, usually the source file has a problem, you can contact the author, but this is not wise, the following provides three ways to read the file.

1. 修改源码 Modify the xlrd source code

first find the compdoc.py file

located at:

Python\lib\site-packages\xlrd\compdoc.py

Find 426 lines of code and comment it out.

找到 426 行, 注释掉

raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s]))

2. Use xlwings instead

    
    import xlwings as xw
    
    try:
        app = xw.App(visible=False, add_book=False)
        xls = app.books.open(excel_file)
    except:
        return
    sheet = xls.sheets[0]
    
    info = sheet.used_range
    
    nrows = info.last_cell.row
    ncols = info.last_cell.column
    
Buy me a 肥仔水!