'How to setting multiple-indentation paragraph in table using python
I have trouble with setting multi-indentation paragraphs in the table.
My code:
import docx
from docx.shared import Cm, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_ALIGN_VERTICAL
document = docx.Document()
table = document.add_table(rows = 1, cols = 3, style = 'Table Grid')
# Create row_0
table_rows1 = table.rows[0]
first_merge = table_rows1.cells[0].merge(table_rows1.cells[1])
second_merge = first_merge.merge(table_rows1.cells[2])
table_cell1 = table.rows[0].cells[0]
paragraph = table_cell1.paragraphs[0]
title = paragraph.add_run("TITLE").bold = True
table_cell1.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table_cell1.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
# Create row_1
add_rows = table.add_row().cells
table_cell2 = table.rows[1].cells[0]
paragraph2 = table_cell2.paragraphs[0]
data2 = paragraph2.add_run(f"\n Lorem")
table.cell(1, 0).width = Cm(5)
table_cell2.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
table_cell3 = table.rows[1].cells[1]
paragraph3 = table_cell3.paragraphs[0]
data3 = paragraph3.add_run('\n:')
table.cell(1, 1).width = Cm(0)
table_cell2.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
table_cell4 = table.rows[1].cells[2]
paragraph4 = table_cell4.paragraphs[0]
paragraph5 = table_cell4.paragraphs[0]
paragraph6 = table_cell4.paragraphs[0]
data4 = paragraph4.add_run("Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum\n")
data5 = paragraph5.add_run("\n1. Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum\n")
data6 = paragraph6.add_run("\n a. Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum")
table_cell4.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
paragraph5.paragraph_format.left_indent = Cm(0.5)
paragraph5.paragraph_format.first_line_indent = Cm(-0.9)
table.cell(1, 2).width = Cm(40)
document.save('asdfasdf.doc')
Result:
Even though, I only changed the indent on data5, why did everything change too? Whereas this works if I do it in a paragraph without a table.
The result that I want:
So, can python-docx handle this? or can oxml handle this? If possible, can you give me a code that I can try? I really can't code with oxml
Thank you!
Solution 1:[1]
Try removing the justify style
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | yldm |


