Kumar Govind
Kumar Govind

Reputation: 235

how to read a xlsx stream file using openpyxl

I am trying to read a streamed xlsx file. The user inputs the data via user interface and then the data is passed to me via streaming in xlsx. How do i read it ? I couldn't find any documentation.

cat text.xlsx | python myprogram.py

How do i read this stream ? Any help would be appreciated. I am not sure whether openpyxl allows this read. I am using python3

Upvotes: 3

Views: 6986

Answers (2)

Andreas
Andreas

Reputation: 83

I do not have enough rep to comment, therefore I am posting this as an answer.

As @CheradenineZK mentions, if the input is coming from an (Azure) blob storage, it needs to be wrapped in a byte buffer. I was struggling to figure out exactly how to do this...

import io
import openpyxl

bytes_in = io.BytesIO(myblob.read())
wb = openpyxl.load_workbook(bytes_in)

is doing the trick for me.

Upvotes: 4

Norrius
Norrius

Reputation: 7920

openpyxl.load_workbook's first argument filename can be not only a filename, but also a file-like object, and sys.stdin is the file-like object representing your program's stdin.

You need it in binary mode though, see the note in the docs regarding binary standard streams.

import sys
from openpyxl import load_workbook

wb = load_workbook(sys.stdin.buffer)
print(wb.sheetnames)

Run:

$ cat test.xlsx | python test.py
['Sheet1', 'Sheet2']

Upvotes: 6

Related Questions