【发布时间】:2019-01-18 05:47:07
【问题描述】:
我想右对齐使用 python-docx 创建的 Word 文档的表格单元格内的文本。我关注了this advice,但它的问题是在文本之前的单元格内添加了一个新行,因此垂直对齐被破坏了。
这是没有右对齐设置的代码:
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[3].text = entry['subtotal']
这是右对齐设置的代码:
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].add_paragraph(entry['unit_price_label']).alignment = WD_ALIGN_PARAGRAPH.RIGHT
row_cells[3].add_paragraph(entry['subtotal']).alignment = WD_ALIGN_PARAGRAPH.RIGHT
以及生成的文档:
在使用 python-docx 右对齐表格单元格时,有什么方法可以避免这种回车?
【问题讨论】:
标签: python python-docx