我必须使用python动态创建word文档。我是通过动态添加表行来做到这一点的,因为它依赖于特定的数据,因此无法知道一个页面上适合的记录有多少。
我需要知道添加到文档中的新元素(表、行或段落)何时会导致一个新页面,这样我就可以相应地在数据库中记录一些数据和每个页面包含的信息。
这是使用python生成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
谢谢你的帮助。
发布于 2019-01-25 20:06:58
在python-docx
中检测自动(呈现器生成的)分页符是不可能的,因为这些中断没有可靠地记录在XML中。
根据您的.docx文件的来源,您可能能够找到上次呈现的页面中断的一些指示。否则,您可能需要使用Microsoft接口来获得对活动呈现程序的访问,该呈现程序可能会为您提供这些信息。注根据运行的机器Word,分页位置可能会发生变化,这取决于字体度量和打印机驱动程序等因素。
在其他问题和答案中也出现了这种情况。这个可能是一个很好的起点:Page number python-docx
要查看其余部分,请在“python分页”上搜索,您将看到很多。方括号内的部分将结果限制为标记为“python”的部分。
https://stackoverflow.com/questions/54371915
复制相似问题