how to output xlsx generated by Openpyxl to browser?

家住魔仙堡 提交于 2019-12-07 03:46:01

问题


I was using stackoverflow for a while now and it helped me very often. Now I have a problem I couldn't solve myself or through searching. I'm trying to output my excel file generated by openpyxl in browser as I was doing it with phpexcel. The method appears to be the same, but I only get broken file. My code looks like this:

from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.writer.excel import save_virtual_workbook
from openpyxl.cell import get_column_letter
from StringIO import StringIO

print 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
print 'Content-Disposition: attachment;filename="results.xlsx"'
print 'Cache-Control: max-age=0\n'

output = StringIO()

wb = Workbook()

ws = wb.worksheets[0]

ws.cell('A1').value = 3.14

wb.save(output)
print output.getvalue()
#print save_virtual_workbook(wb)

I use the version 1.5.8 and python 2.7. None of the approaches works. When I just use it from desktop and not browser it works flawlessly. I would be very thankful for help.

P.S. please don't tell me that using other language or program would be easier. I need to solve this with python.


回答1:


this is work for me. I use python 2.7 and latest openpyxl and send_file from flask

... code ...

import StringIO
from openpyxl import Workbook
wb = Workbook()
ws = wb.active # worksheet
ws.title = "Excel Using Openpyxl"
c = ws.cell(row=5, column=5)
c.value = "Hi on 5,5"
out = StringIO.StringIO()
wb.save(out)
out.seek(0)

return send_file(out, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            attachment_filename='xxl.xlsx', as_attachment=True)



回答2:


output = HttpResponse(mimetype='application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
file_name = "Test.xlsx"
output['Content-Disposition'] = 'attachment; filename='+ file_name

wb = Workbook()

ws = wb.worksheets[0]

ws.cell('A1').value = 3.14

wb.save(output)

return output

I used this tips to download my files with openpyxl. Hope that will help




回答3:


Writing the xlsx output to disk and then serving it up via Apache worked perfectly, but putting it out directly caused errors in Excel and other issues.

I added a couple of extra steps and made one minor change to your code:

buffer=output.getvalue()

In the HTTP headers:

print "Content-Length: " + str(len(buffer))

And used write() instead of print() to push the buffer into the standard output stream:

stdout.write(buffer)



回答4:


Your scripts works for me as you expect without alterations.

I can only assume you have a problem with your cgi script setup.

Make sure you have the directory where the script lives actually gets served by the web server. On apache you can achieve this with:

  ScriptAlias /cgi-bin/ /home/WWW/localhost/cgi-bin/

Make sure the script is excutable by setting the script permissions. For commandline operation (python scriptname) that was not necessary, for your webbrowser that is. And make sure the owner of the webserver can excute the scripts, as the webserver probably does not run as you.




回答5:


Because Excel uses a binary format you should be using BytesIO to buffer.

from io import BytesIO

But what error are you getting if you use save_virtual_workbook() which does this for you?




回答6:


If you want to build an HTML table that looks like your spreadsheet, you probably want to work with CSV. Either do this, instead of Excel, OR convert your Excel to CSV after you build it.

In any case, once you have the data in CSV format, then it's simply a matter of using python to build the HTML page and looping through the CSV data, while inserting your <table>, <tr>, and <td> tags, as appropriate.



来源:https://stackoverflow.com/questions/12287199/how-to-output-xlsx-generated-by-openpyxl-to-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!