简体   繁体   中英

Python read SAS generated XML type .xls file

I am trying to extract tabs from hundreds of SAS generated .xls files. I tried the following approach without luck. My version of xlrd is 0.9.2.

import xlrd 
book = xlrd.open_workbook('out_1.xls')

The error message is:

Traceback (most recent call last):[Finished in 0.2s with exit code 1]
  File "I:\Dropbox\Sas data\sacwin\test.py", line 3, in <module>
    book = xlrd.open_workbook('out_1.xls') # Open an .xls file
  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 435, in open_workbook
    ragged_rows=ragged_rows,
  File "C:\Python27\lib\site-packages\xlrd\book.py", line 91, in open_workbook_xls
    biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
  File "C:\Python27\lib\site-packages\xlrd\book.py", line 1258, in getbof
    bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
  File "C:\Python27\lib\site-packages\xlrd\book.py", line 1252, in bof_error
    raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '<?xml ve'

Once I opened the .xls file in an editor the header looks like:

<?xml version="1.0" encoding="windows-1252"?>

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office">

Would you mind giving me some suggestions on how to parse these files? Thanks!

I'm looking for a solution to this problem as well. I can tell you that the file format is xml but pre-dates Excel 2007 'Office Open XML (ECMA-376)' format (I think it's SpreadsheetML), so it's not supported by xlrd.

If there's no python library available and you have good prior knowledge of the structure of the files you need to process I'd just use an xml reader.

Regards Dave

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