Skip to main content

A Fusion server that will transform your py3o.template into final LibreOffice documents

Project description

Introduction

py3o.fusion is a web server that provides a simple but important service: transform your py3o.template LibreOffice templates into final LibreOffice documents.

This is intended to avoid direct dependencies in your own applications. This also opens up the py3o ecosystem to other programming languages than Python.

Using it

You can use any language. Here is an example using python requests:

# 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 = 'http://localhost: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'),
        'img_logo': open('images/new_logo.png', 'rb'),
    }

    # fusion API needs those 3 keys
    fields = {
        "targetformat": targetformat,
        "datadict": data_s,
        "image_mapping": json.dumps({"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['img_logo'].close()

And voila. You have a file called out.odt that contains the final odt fusionned with your datadictionnary.

For the full source code + template file and images just download them from our repo

If you just want to test it rapidly you can also point your browser to the server http://localhost:8765/form and fill the form manually.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

py3o.fusion-0.1.zip (267.5 kB view details)

Uploaded Source

py3o.fusion-0.1.tar.gz (257.6 kB view details)

Uploaded Source

Built Distributions

py3o.fusion-0.1-py2.7.egg (269.8 kB view details)

Uploaded Source

py3o.fusion-0.1-py2-none-any.whl (265.2 kB view details)

Uploaded Python 2

File details

Details for the file py3o.fusion-0.1.zip.

File metadata

  • Download URL: py3o.fusion-0.1.zip
  • Upload date:
  • Size: 267.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py3o.fusion-0.1.zip
Algorithm Hash digest
SHA256 db20027f437e4b4edfdc28bec46c0dc52e74511cb84551dbd9b777205d2d0367
MD5 212172c78c84c4ed8fa7327615017af5
BLAKE2b-256 4830e94b5c0d5da02743bf237dfd57d575d4eedbafd6ea4fa86f6b49e6adb01a

See more details on using hashes here.

Provenance

File details

Details for the file py3o.fusion-0.1.tar.gz.

File metadata

  • Download URL: py3o.fusion-0.1.tar.gz
  • Upload date:
  • Size: 257.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py3o.fusion-0.1.tar.gz
Algorithm Hash digest
SHA256 ac7f44cf842a6bec2e2b954b0d76a537881973dca19d3d40156b1f5654fb6ac4
MD5 96e05f991dd9c5662d09e15585e126a5
BLAKE2b-256 7fec3338c8e6ed5188649fee9aa6248bf95d414759e98e2b95e3b047eaaa57fd

See more details on using hashes here.

Provenance

File details

Details for the file py3o.fusion-0.1-py2.7.egg.

File metadata

File hashes

Hashes for py3o.fusion-0.1-py2.7.egg
Algorithm Hash digest
SHA256 88dc6696690eee90fbaaa46d15f4f39c7dc800174a3cd3bc98b934e3f5e0d6fc
MD5 e90b2a3d68f57e0353469dc88ce3a3dd
BLAKE2b-256 59b25fa9175a52c3b8d16cbab004eb08c39e5e6a17882c4e2d61baefbf880251

See more details on using hashes here.

Provenance

File details

Details for the file py3o.fusion-0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for py3o.fusion-0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 d546eedbc3b83f0346330dd40e2b47ad78376f106d10874415bfde5df6d02ada
MD5 d55e288cc24ad793c1bdd314397240cb
BLAKE2b-256 5c8045cab64d8f8b7245b0df897238f41ed3527314dbbe76753c45e706bcf856

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page