0

I'm using a software product that is opening and XLS file and sending the XLS as this string

PK\x03\x04\x14\x00\x06\x00\b\x00\x00\x00!\x00{\x92..

I've tried the below code and get the error Unsupported format, or corrupt file: Expected BOF record; found b'<\xac\xc2\xa2{^\x9e\xd4' [file '/home/vflow/.local/lib/python3.6/site-packages/xlrd/book.py', line 1278]

Question is how to open this in xlsx in python when getting this string?

The XLS can be opened using MSoffice. I created a simple xlsx with the word 'test' in on column as a 2nd try. The software we have serves the above string PK... Note the above excel string starts with PK - which indicates a zip file ( xlsx excel files are zipped btw so it looks ok from a byte string point of view.)

Similar question from which the above approach is sourced. Read byte string as xls file

import xlrd
import base64

    
myvar = data.body  #  contains : PK\x03\x04\x14\x00\x06\x00\b\x00\x00\x00!\x00{\x92..
decoded_bytes = base64.b64decode(myvar) # contains : <\xac¢{^\x9e\xd4\xf2\xa5\xeb1\x9aY\xf4\x13[PPI\xb2..

x = xlrd.open_workbook(file_contents=decoded_bytes)

# Unsupported format, or corrupt file: Expected BOF record; found b'<\xac\xc2\xa2{^\x9e\xd4' [file '/home/vflow/.local/lib/python3.6/site-packages/xlrd/book.py', line 1278]

x = xlrd.open_workbook(file_contents=data.body) # use data.body instead

# Excel xlsx file; not supported [file '/home/vflow/.local/lib/python3.6/site-packages/xlrd/__init__.py', line 170]
ozmike
  • 2,738
  • 1
  • 33
  • 40
  • Take a look on [this](https://stackoverflow.com/a/46214958/10824407) answer under similar question. – Olvin Roght Aug 30 '22 at 06:05
  • Yeah i created a new empty xlsx , just with the word 'test' in the column. The software we are using is serving this. I will need to try a xls (rather than a xlsx). but this does not solve the issue as we need to do xlsx. The excel start with PK - which indicate a zip file ( xlsx excel files are zipped btw so it look ok from a byte string point of view.) – ozmike Aug 30 '22 at 23:44

0 Answers0