pyexcel write to stream

210 Views Asked by At

i'm trying to write to a BytesIO a partial xls file, in order to stream back to the client an excel file.

The problem is that the client gets only the first few rows (first yield iteration to the byteio object)

It looks like the save_to_memory is adding EOF bytes although the stream should not be closed

Example code:

import io
import pyexcel as pe
from aiohttp import web

async def get(request):
  response = web.StreamResponse(headers={'Content-Disposition': 'inline; filename="myfile.xls"'})
  await response.prepare(request)
  response.content_type = 'multipart/x-mixed-replace;boundary=ffserver'
  data = [1,2,3,4]
  mem = io.BytesIO()
  sheet = pe.Sheet(name='ids', colnames=['id'])

  try:
      for id in data:
         sheet.extend_rows([id])
         mem = sheet.save_to_memory("xls", mem)
         await response.write(mem.read())
  finally:
      await response.write_eof()

in this case the only row the client would get is with value 1

1

There are 1 best solutions below

0
On

This is what I did: -

import pyexcel as p

def output_stream():
    # create column titles
    column_names = ['col1', 'col2', 'col3']
    # add some sample data
    data = [['item1', 'item2', 'item3']]
    # insert header at top of file
    data.insert(0, column_names)
    # create sheet
    sheet = p.Sheet(data, 'A')
    # create stream
    stream = sheet.stream.xls

    return stream