跳转到主要内容

将您的py3o.template转换为最终LibreOffice文档的Fusion服务器

项目描述

简介

py3o.fusion是一个提供简单但重要的服务的Web服务器

  • py3o.template LibreOffice模板转换为最终的LibreOffice文档。

  • 将OpenOffice / LibreOffice文档转换为任何支持的格式

基本上,您可以将模板化的OpenOffice / LibreOffice文档转换为任何支持的格式(odt、doc、docx、pdf)

这是为了避免在您的应用程序中直接依赖。这也使得py3o生态系统向其他编程语言开放。

部署

我们建议使用我们创建的Docker镜像。这无疑是快速将完整的转换服务启动和运行的最快方法。

只需按照我们Docker Hub页面上的说明操作。

使用方法

您可以使用任何语言。

以下是可能的最简单示例

# import the wonderful requests lib
# if you need to intall it just try
# pip install --upgrade requests
import requests

# define where is your py3o.fusion endpoint
url = 'https://127.0.0.1:8765/form'

# open up the file and stuff it into a dictionary
# tmpl_file is a required field on the form. If you don't give
# it to the endpoint you'll receive an error back from it.
files = {
    'tmpl_file': open('templates/simple.odt', 'rb')
}

# then prepare the other fields of the form
# those 3 fields are also mandatory and failing to POST
# one of them will get you an error back from the server
#
# In this example you can see we leave the datadictionary
# and the image_mapping empty... This is because we won't
# send a template to the server but a simple plain
# old ODT file
fields = {
    "targetformat": 'pdf',
    "datadict": "{}",
    "image_mapping": "{}",
}

# finally POST our request on the endpoint
r = requests.post(url, data=fields, files=files)

# don't forget to close our orginal odt file
files['tmpl_file'].close()

# see if it is a success or a failure
# ATM the server only returns 400 errors... this may change
if r.status_code == 400:
    # server says we have an error...
    # this means it properly catched the error and nicely
    # gave us some info in a JSON answer...
    # let's use this fact
    print r.json()

else:
    # if we're here it is because we should receive a new
    # file back

    # let's stream the file back here...
    chunk_size = 1024

    # fusion server will stream an ODT file content back
    outname = 'request_out.%s' % 'pdf'
    with open(outname, 'wb') as fd:
        for chunk in r.iter_content(chunk_size):
            fd.write(chunk)

    # warn our dear user his file is ready
    print "Your file: %s is ready" % outname

在此处抓取完整的 odt2pdf.py 源代码示例 ODT 文件,以下是一个一步完成的示例

$ mkdir -p templates && wget https://bitbucket.org/faide/py3o.fusion/raw/055770694c0c4c1593aed156149d2d43a6042913/py3o/fusion/static/examples/odt2pdf.py && wget https://bitbucket.org/faide/py3o.fusion/src/6817b8bde3895434ed1997b07a1c422e66c033b3/py3o/fusion/static/examples/templates/simple.odt && mv simple.odt templates/

以下是一个更复杂的示例,它使用 py3o.template 将数据字典融合到模板 ODT 中,并返回结果 PDF。您会注意到您还可以覆盖模板中的图像

# you'll need to install requests to make this example work
# pip install --upgrade requests
# should do the trick
import requests
import json

# point the client to your own py3o.fusion server
url = 'https://127.0.0.1:8765/form'

# target formats you want... can be ODT, PDF, DOC, DOCX
targetformats = ["odt", "pdf", "doc", "docx"]


class MyEncoder1(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Item):
            obj = obj._asdict()
        else:
            obj = super(MyEncoder1, self).default(obj)

        return obj


class Item(object):
    def _asdict(self):
        return self.__dict__


items = list()

item1 = Item()
item1.val1 = 'Item1 Value1'
item1.val2 = 'Item1 Value2'
item1.val3 = 'Item1 Value3'
item1.Currency = 'EUR'
item1.Amount = '12345.35'
item1.InvoiceRef = '#1234'
items.append(item1)

for i in xrange(1000):
    item = Item()
    item.val1 = 'Item%s Value1' % i
    item.val2 = 'Item%s Value2' % i
    item.val3 = 'Item%s Value3' % i
    item.Currency = 'EUR'
    item.Amount = '6666.77'
    item.InvoiceRef = 'Reference #%04d' % i
    items.append(item)

document = Item()
document.total = '9999999999999.999'

data = dict(items=items, document=document)

data_s = json.dumps(data, cls=MyEncoder1)

for targetformat in targetformats:
    # open the files you need
    files = {
        'tmpl_file': open('templates/py3o_example_template.odt', 'rb'),
        'staticimage.img_logo': open('images/new_logo.png', 'rb'),
    }

    # fusion API needs those 3 keys
    fields = {
        "targetformat": targetformat,
        "datadict": data_s,
        "image_mapping": json.dumps({"staticimage.img_logo": "logo"}),
    }

    # and it needs to receive a POST with fields and files
    r = requests.post(url, data=fields, files=files)

    # TODO: handle error codes
    if r.status_code == 400:
        # server says we have a problem...
        # let's give the info back to our human friend
        print r.json()

    else:
        chunk_size = 1024
        # fusion server will stream an ODT file content back
        ext = targetformat.lower()
        with open('request_out.%s' % ext, 'wb') as fd:
            for chunk in r.iter_content(chunk_size):
                fd.write(chunk)

    files['tmpl_file'].close()
    files['staticimage.img_logo'].close()

好了。您有一个名为 out.odt 的文件,其中包含与您的数据字典融合后的最终 odt 文件。

要获取完整的源代码、模板文件和图像,请从 我们的仓库 下载

如果您只想快速测试,也可以将浏览器指向服务器 https://127.0.0.1:8765/form 并手动填写表单。

更新日志

0.8.9 2018年7月8日

  • 添加了对 PDF 导出选项的支持

  • 修复了 -s 命令行选项

0.8.8 2018年4月11日

  • 添加了命令行选项 (-s),以仅监听特定的网络接口(感谢 Alexis de Lattre)

0.8.7 2017年4月5日

  • 引入了表单选项,以控制布尔值逃逸和未定义变量逃逸

0.8.6 2016年11月29日

  • 添加 py3o.types 作为依赖项

  • 更新了示例 odt

0.8 2015年6月26日

  • 添加了新的格式(py3o.formats)支持,而不是使用硬编码的值,与旧格式兼容,因此客户端无需修改代码

  • 在表单页面添加了更多关于当前支持格式的信息。信息是动态计算的,并考虑了您是否有渲染服务器

  • 在页脚中添加了服务器版本

0.8 2015年6月3日

  • 修复了在 0.7 版本中引入的回归问题,该问题涉及存在渲染服务器时允许的格式计算。所有希望使用渲染服务器(即:生成非原生格式)的 0.7 用户应升级到 0.8

0.7 2015年6月2日

  • 内部重构,同时更改了公共 API,现在使用 py3o.formats 而不是使用内部函数来处理格式。这更改了用户必须提供的格式名称,现在是小写而不是大写。有关所有支持格式及其名称的更多信息,请参阅 https://bitbucket.org/faide/py3o.formats

0.6 2015年5月29日

  • 现在可以优雅地处理调用者未提供 json 有效负载的情况

0.5 2014年10月15日

  • 添加了更好的日志(日期时间、级别、模块、消息)

  • 修复了由 skipflag 破坏的本地格式渲染问题

0.4 2014年10月14日

  • 在功能页面添加了语法着色

  • 添加了新的关键字到 POST 选项以跳过融合步骤(即:py3o.template -> plain odt)。这是因为在某些情况下,您只想将现有的 ODT 文件转换为目标格式。

0.3 2014年9月12日

  • 添加了可以从服务器自身功能页面下载的示例

0.2 2014年9月11日

  • 修复了调用者指定无效图像映射的错误情况。错误在服务器上被捕获,但没有发送回客户端。

0.1 2014年9月11日

  • 初始发布

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

py3o.fusion-0.8.9.tar.gz (736.2 kB 查看哈希值)

上传时间 源代码

构建的分发

py3o.fusion-0.8.9-py2.7.egg (1.2 MB 查看哈希值)

上传时间 源代码

py3o.fusion-0.8.9-py2-none-any.whl (1.2 MB 查看哈希值)

上传时间 Python 2

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面