3

Python program opens .xls just fine but will not open .xlsm and immediately fails.

xlrd.open_workbook("Some filename.xlsm")  

Error: Exception has occurred: AttributeError 'bytes' object has no attribute 'seek'

Any help would be greatly appreciated...

Tim Wiley
  • 229
  • 2
  • 8
  • An xlsm file is not the same as an xls file; why would you assume it would work? – Don Simon Mar 11 '19 at 20:37
  • Because all the documentation online and examples everywhere says it normally does and many people are well past this... xlsm is just another version of the excel document so xlrd via the open_workbook command doesn't say anything special about different versions and indicates it should work on most all versions. – Tim Wiley Mar 11 '19 at 20:43
  • 1
    The [xlrd docs](https://xlrd.readthedocs.io/en/latest/index.html) state: xlrd is a library for reading data and formatting information from Excel files, whether they are .xls or .xlsx files. They don't mention .xlsm at all. – jfaccioni Mar 11 '19 at 20:45
  • They specifically exclude macros in their documentation. – Don Simon Mar 11 '19 at 20:46
  • 1
    Got it to work... i need to read from a file loaded into memory first rather than the native open_workbook which by default reads from file... (Thanks for all the wonderful wonderful help jfaccioni and Don .... Great stuff really... – Tim Wiley Mar 11 '19 at 20:50
  • Does this answer your question? [Pandas cannot open an Excel (.xlsx) file](https://stackoverflow.com/questions/65250207/pandas-cannot-open-an-excel-xlsx-file) – Chris Withers Dec 16 '20 at 07:40

2 Answers2

1

For those who run into this. The solution is to read the file via binary into a variable to avoid the open_workbook library for loading the file... it has something to do with the file coded utf-16-le i think... anyways here is a snippet of what needs to happen.

with open(FilePath, 'rb') as tmp_file:
   tmp_excel=tmp_file.read()
tmp_file.close

objExcel=xlrd.open_workbook(file_contents=tmp_excel)
Tim Wiley
  • 229
  • 2
  • 8
-1

In xlrd there is a test of the file type. Somehow the file type of an .xlsm document is not '.xls'. For version 1.2.0 of xlrd this not an issue.

Harm Salomons
  • 127
  • 2
  • 2