English 中文(简体)
引言语:
原标题:Generating word docs with Flask?

I m试图搭建一个单页薄片,使用户能够下载一个字页文件。 我已经指出,如何使用假思-思想制作/回避文件,但现在我需要在答复中提供该文件。 任何想法?

这里我迄今为止所做的事情:

from flask import Flask, render_template
from docx import Document
from cStringIO import StringIO

@app.route( / )
def index():
    document = Document()
    document.add_heading("Sample Press Release", 0)
    f = StringIO()
    document.save(f)
    length = f.tell()
    f.seek(0)
    return render_template( index.html )
最佳回答

页: 1

from flask import Flask, render_template, send_file
from docx import Document
from cStringIO import StringIO

@app.route( / )
def index():
    document = Document()
    document.add_heading("Sample Press Release", 0)
    f = StringIO()
    document.save(f)
    length = f.tell()
    f.seek(0)
    return send_file(f, as_attachment=True, attachment_filename= report.doc )
问题回答

www.un.org/spanish/ecosoc 转过我的人:

提及这两个联系:

io.StringIO 现在改为<代码>cStringIO。

also it will raise an error as document.save(f) should receive a path or binary file

<>strong>code should be such:

from flask import Flask, render_template, send_file
from docx import Document
from io import BytesIO

@app.route( / )
def index():
    document = Document()
    f = BytesIO()
    # do staff with document
    document.save(f)
    f.seek(0)

    return send_file(
        f,
        as_attachment=True,
        # Use attachment_name for Flask version < 2.2.0
        download_name= report.docx 
    )





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签