Reading Excel in Python

UPDATE@201801: 好早的文章,从 Python 这个文档里面移出来。


从文档里面可以看到它是直接分析excel文件的,通过阅读OpenOffice的关于M$ Excel文件格式文档编写的。

Development of this module would not have been possible without the document "'s Documentation of the Microsoft Excel File Format" ("OOo docs" for short). The latest version is available from in PDF format and XML format. Small portions of the OOo docs are reproduced in this document. A study of the OOo docs is recommended for those who wish a deeper understanding of the Excel file layout than the xlrd docs can provide.



  1. Support for handling dates, and documentation of Excel date problems and how to avoid them.
  2. Unicode aware; correctly handles "compressed" Unicode in modern files; decodes legacy charsets in older files (if Python has the codec).
  3. Extracts all data (including Booleans and error-values)
  4. Extracts named references
  5. Extracts formatting information for cells (number format, font, alignment, borders, backgrounds, protection) and rows/columns (default height/width, etc). This effort was funded by Simplistix Ltd.
  6. Further information: APIs, README, HISTORY


Exclusions: xlrd will not attempt to decode password-protected (encrypted) files. Otherwise, it will safely and reliably ignore any of these if present:

  1. Charts, Macros, Pictures, any other embedded object. WARNING: currently this includes embedded worksheets.
  2. Visual Basic (VBA) modules
  3. Formulas (results of formula calculations are extracted, of course)
  4. Comments and hyperlinks


#!/usr/bin/env python
#Copyright (C) dirlt

import xlrd

print '--------------------sheets:#%d--------------------'%(book.nsheets)
for i in range(0,book.nsheets):
    print '----------sheet%d:\'%s\', rows:%d, cols:%d----------'%(i,,sheet.nrows,sheet.ncols)
    for r in range(0,sheet.nrows):
        for c in range(0,sheet.ncols):
                # access cell.ctype.
                print '%s '%(cell.value),
        print ''