Alexander Ovchinnikov
Alexander Ovchinnikov

Reputation: 389

how to generate and print documents (python)

I need to allow users edit and print client-related documents (invoices and another).

User fill print form (select clients, month, how much copies of each of 4 types of documents he/she needs) and click to print button.

Next my system should do (algorithm):

result = create new word-friendly file  # because user may manually edit it later

for client in form_clients:

    snapshot = select row in snapshots table 
               where client == client and month == form_month

    document1 = generate_from_template(snapshot, tpl1.docx)
    for 1 to form_how_much_copies_of_1_type_of_document_he_or_she_needs:
        result += document1

    document2 = generate_from_template(snapshot, tpl2.docx)
    for 1 to form_how_much_copies_of_2_type_of_document_he_or_she_needs:
        result += document2

    document3 = generate_from_template(snapshot, tpl3.docx)
    for 1 to form_how_much_copies_of_3_type_of_document_he_or_she_needs:
        result += document3

    document4 = generate_from_template(snapshot, tpl4.docx)
    for 1 to form_how_much_copies_of_4_type_of_document_he_or_she_needs:
        result += document4

print result

requirements:

My question is how to generate this file (result)? There is https://github.com/mikemaccana/python-docx project, but not sure about it can work with templates (or at least generate tables as I need)... Or I can save all my .docx templates as html and use them as normal django templates but not sure how to combine them together in 1 document and create page breaks... Or may be i should look at another word-compatible file formats?...

p.s. better use python but its not critical, i may use java/perl/ruby/php/bash/etc.. and install any new Ubuntu-compatible packages...

Upvotes: 5

Views: 11742

Answers (1)

Lionel
Lionel

Reputation: 3288

If using a template is not absolutely required, or if you are willing to recreate the template with code, use reportlab for python to generate a PDF, then print it.

See this example on how to make tables: http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/

Also check out this invoice example: https://github.com/radzhome/fedex-commercial-invoice

Upvotes: 5

Related Questions