I want to right align the text inside a table cell of a Word document created with python-docx. I followed this advice, but the problem with it is that a new line is added inside de cell before the text, so the vertical alignment is broken.
This is the code without the right alignment setting:
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']
And this is the resulting document:
This is the code with the right alignment setting:
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
And the resulting document:
Is there any way to avoid this carriage return when right aligning table cells with python-docx?