【问题标题】:how to know when a new paragraph in python-docx causes a new page如何知道 python-docx 中的新段落何时导致新页面
【发布时间】:2019-01-25 19:47:28
【问题描述】:

我必须使用 python-docx 动态创建 word 文档。我通过动态添加表格行来做到这一点,因为它取决于特定的数据,所以无法知道页面上有多少记录。

我需要知道何时添加到文档中的新元素(表格行或段落)会导致一个新页面,因此我可以根据每个页面包含的信息在数据库中记录一些数据。

这是用python-docx生成word文档的代码:

def get_invoice_word_report(self, request, invoices_controllers):
    import unicodedata
    from django.core.files import File
    from docx import Document
    from docx.shared import Inches, Pt
    from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK
    from docx.enum.table import WD_ALIGN_VERTICAL
    from docx.enum.table import WD_TABLE_ALIGNMENT

    document = Document()

    section = document.sections[-1]
    section.left_margin = Inches(0.5)
    section.right_margin = Inches(0.5)

    style = document.styles['Normal']
    font = style.font
    font.name ='Arial'
    font.size = Pt(8)

    i = 0
    for invoices_controller in invoices_controllers:
        context = invoices_controller.get_context()
        if i > 0:
            run.add_break(WD_BREAK.PAGE)
            if i == len(invoices_controllers) - 1:
                last = context['invoices']['invoice_number']
        else:
            first = context['invoices']['invoice_number']

        document.add_paragraph("Invoice".format(context['invoices']['invoice_number'])).alignment = WD_ALIGN_PARAGRAPH.RIGHT
        document.add_paragraph("Folio {}".format(context['invoices']['invoice_number'])).alignment = WD_ALIGN_PARAGRAPH.RIGHT
        document.add_paragraph(context['invoices']['agency']['company']['name'])
        document.add_paragraph(context['invoices']['agency']['company']['address'])
        date = context['invoices']['period_end_date'].split('-')
        document.add_paragraph("{}      {}      {}".format(date[2], date[1], date[0])).alignment = WD_ALIGN_PARAGRAPH.RIGHT
        document.add_paragraph(context['invoices']['line'])
        document.add_paragraph(context['invoices']['text'])

        table = document.add_table(rows=1, cols=4)
        hdr_cells = table.rows[0].cells

        hdr_cells[0].width = Inches(0.1)
        hdr_cells[1].width = Inches(10)
        hdr_cells[2].width = Inches(1)
        hdr_cells[3].width = Inches(1)

        for entry in context['invoices']['entries']:
            row_cells = table.add_row().cells
            row_cells[0].text = str(entry['amount'])
            row_cells[1].text = entry['line']
            row_cells[2].text = entry['unit_price_label']
            row_cells[2].paragraphs[0].alignment= WD_ALIGN_PARAGRAPH.RIGHT
            row_cells[3].text = entry['subtotal']
            row_cells[3].paragraphs[0].alignment= WD_ALIGN_PARAGRAPH.RIGHT

            if entry['text']:
                text_cells = table.add_row().cells
                text_cells[1].text = entry['text']

        row_cells = table.add_row().cells
        row_cells[0].text = ''
        row_cells[1].text = ''
        row_cells[2].text = ''
        row_cells[3].text = context['total']
        row_cells[3].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT

        row_cells = table.add_row().cells
        row_cells[0].text = ''
        row_cells[1].text = ''
        row_cells[2].text = ''
        row_cells[3].text = '$0.00'
        row_cells[3].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT

        row_cells = table.add_row().cells
        row_cells[0].text = ''
        row_cells[1].text = ''
        row_cells[2].text = ''
        row_cells[3].text = context['total']
        row_cells[3].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT

        run = document.add_paragraph("Son {}".format(context['total_text'])).add_run()
        i += 1

    current_directory = settings.MEDIA_DIR
    if len(invoices_controllers) > 1:
        file_name = "Invoices {}-{}.docx".format(first, last)
    else:
        file_name = "Invoice {}.docx".format(first)
    document.save(current_directory + file_name)

    return request.get_host()+ settings.MEDIA_URL + file_name

感谢您的帮助。

【问题讨论】:

    标签: python python-docx


    【解决方案1】:

    无法检测python-docx 中的自动(渲染器生成)分页符,因为这些分页符没有可靠地记录在 XML 中。

    根据您的 .docx 文件的来源,您可能会找到 一些 指示上次呈现的分页符。否则,您可能需要使用 Microsoft VBA 界面来访问可以为您提供此信息的实时渲染器。请注意,分页符位置可能会根据运行 Word 的机器而发生变化,具体取决于字体指标和打印机驱动程序等因素。

    这已经出现在其他问题和答案中。这可能是一个不错的起点:Page number python-docx

    要查看其余内容,请搜索“[python-docx] 分页符”,您会发现有很多。方括号部分将结果限制为带有“python-docx”标记的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2015-05-22
      相关资源
      最近更新 更多