python - 如何生成和打印文档(python)

标签 python printing

我需要允许用户编辑和打印与客户相关的文档(发票等)。

用户填写打印表格(选择客户、月份、他/她需要的 4 类文件各份的份数)并点击打印按钮。

接下来我的系统应该做的(算法):

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

要求:

  • 能够编辑结果(需要友好的格式)
  • 分页符(新页面上的每个文档)
  • 表格,不是很简单,看invoice tpl例子: tpl example
  • 字体大小和样式(例如粗体)
  • 保存文本位置

我的问题是如何生成这个文件(结果)? 有 https://github.com/mikemaccana/python-docx项目,但不确定它是否可以使用模板(或至少根据需要生成表格)... 或者我可以将我所有的 .docx 模板保存为 html 并将它们用作普通的 django 模板,但不确定如何将它们组合在 1 个文档中并创建分页符...... 还是我应该看看另一种与 word 兼容的文件格式?...

附注最好使用 python 但它并不重要,我可以使用 java/perl/ruby/php/bash/etc.. 并安装任何新的 Ubuntu 兼容包...

最佳答案

如果不是绝对需要使用模板,或者如果您愿意使用代码重新创建模板,请使用 reportlab让 python 生成 PDF,然后打印它。

请参阅此示例,了解如何制作表格: http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/

另请查看此发票示例: https://github.com/radzhome/fedex-commercial-invoice

关于python - 如何生成和打印文档(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7831328/

相关文章:

用于 ERP 开发的 python Web 框架

linux - GTK-打印 : detect if printer is configured/printing would work?

css - 打印 html 页面 - 分页

java - 如何在 java fx 中显示打印对话框并打印文档(不仅仅是节点)

java - 从服务器端触发网页打印

python - 收到打印作业 Python

python - 图例颜色与 seaborn 不匹配

python - Flask - 返回 jsonify 与 dict 时有什么区别?

python - 仅对非零值运行 df.describe()

Python 记录子进程