下载带有Cherrypy的docxtpl生成的文件 [英] Download docxtpl generated file with cherrypy

查看:85
本文介绍了下载带有Cherrypy的docxtpl生成的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docxtpl生成Word文档,并且想知道一旦使用cherrypy生成的用户如何下载该文件,请参见下面的代码.

I am using docxtpl to generate a word document, and wondering how a user can download this file once generated using cherrypy, please see my code below.

我唯一能想到的解决方案是将其保存到www文件夹并创建指向该位置的链接,但是我敢肯定,这可以简化.

the only solution i could come up with is to save it to the www folder and create a link to the location, but i am sure this can be simplified.

代码:

import os, os.path
import random
import string
import cherrypy
from docxtpl import DocxTemplate
import sys
from auth import AuthController, require, member_of, name_is
import socket

reload(sys)  
sys.setdefaultencoding('utf8')
cherrypy.config.update({'server.socket_port': 8000})
cherrypy.server.socket_host = '0.0.0.0'
cherrypy.engine.restart()

class Root(object):

    _cp_config = {
        'tools.sessions.on': True,
        'tools.auth.on': True    }

    @cherrypy.expose()
    def default(self, **kwargs):
        print kwargs    
        if kwargs.get('csa_no'):
#             print kwargs.get('csa_no')
            tpl=DocxTemplate('csa_tpl.docx')            
            sd = tpl.new_subdoc()
            p = sd.add_paragraph('This 1st insert')
            sd2 = tpl.new_subdoc()
            p = sd2.add_paragraph('This 2nd insert')
            context1 = {
                    'date': 'jkh',
                    'company_name' : 'Test Footer',
                    'cost' : '10,000',
                    'project_description': kwargs['project_description'],
                    'site': kwargs['site'],
                    'sp': kwargs['sp'],
                    'wo': kwargs['WO'],
                    'contract_manager': kwargs['contract_manager'],
                    'csa_no': kwargs['csa_no'],
                    'variation_reason': kwargs['variation_reason'],
                    'variation_scope_of_works': kwargs['variation_scope_of_works'],

                    'Initial_Contract_Value': '$300,000',
                    'variation_total': '$20,000',
                    'Ammended_Contract_Value': '$320,000',
                    'project_manager': kwargs['project_manager'],
                    'construction_manager': 'Dane Wilson',
                    'date': '30/04/2016',    
                }
            tpl.render(context1)
            file_name_with_path = '/var/www/html/csa_documents/' + kwargs['sp'] + '-'+ kwargs['WO'] + '_' + kwargs['site'] + '_-_' + 'CSA' + kwargs['csa_no'] +'.docx'
            file_name = kwargs['sp'] + '-'+ kwargs['WO'] + '_' + kwargs['site'] + '_-_' + 'CSA' + kwargs['csa_no'] +'.docx'
            print file_name
            print file_name_with_path
            tpl.save(file_name_with_path)
            return '''

            <!DOCTYPE html>
            <html>
            <head>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta charset="utf-8" http-equiv="X-UA-Compatible" content="IE=9" />
            <link href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.css" rel="stylesheet">
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script src="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
            <title>Broadspectrum</title>
            </head>
            <body>
            <div data-role="header" data-theme="b">
            <h1>TCSS Gateway</h1>
            </div>
            <h2>Success</h2>
            <a href="http://192.168.1.7">another submission</a>
            <a href="http://192.168.1.7/csa_documents/%s">Download & Review CSA Document</a>
            </body>

            ''' % file_name

推荐答案

简短的答案是,基本上,您需要编写一些内存流(例如 BytesIO ),并预先设置一些HTTP标头并返回 file_generator .您的问题与一个月前提出的问题几乎相同,并且此处是我的回答.

The short answer is that basically you need to write some in-memory stream (e.g. BytesIO), pre-set some HTTP headers and return a file_generator. Your question is almost the same as one asked a month ago and here is my answer to it.

我在python3中为您起了一个小片段:

I've drafted a little snippet for you in python3:

from io import BytesIO

import cherrypy
from cherrypy.lib import file_generator

from docxtpl import DocxTemplate


class GenerateDocx:
    @cherrypy.expose
    def build_docx(self, *args, **kwargs):
        iostream = BytesIO()

        tpl = DocxTemplate('csa_tpl.docx')
        ...
        # build your tpl here
        ...
        tpl.get_docx().save(iostream)

        cherrypy.response.headers['Content-Type'] = (
            # set the correct MIME type for docx
            'application/vnd.openxmlformats-officedocument'
            '.wordprocessingml.document'
        )
        cherrypy.response.headers['Content-Disposition'] = (
            'attachment; filename={fname}.docx'.format(
                fname='put your file name here'
            )
        )

        iostream.seek(0)
        return file_generator(iostream)

UPD:我刚刚检查了如果处理程序的返回值具有 read()方法支持

UPD: I've just checked that the response body gets automatically wrapped with file_generator if the return value of a handler has read() method support

这篇关于下载带有Cherrypy的docxtpl生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆