11

So I am starting to use pythons docx library. Now, I create a table with multiple rows, and only 2 columns, it looks like this:

Now, I would like the text in those cells to be centered horizontally. How can I do this? I've searched through docx API documentation but I only saw information about aligning paragraphs.

martineau
  • 119,623
  • 25
  • 170
  • 301
minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57

6 Answers6

19

There is a code to do this by setting the alignment as you create cells.

doc=Document()
table = doc.add_table(rows=0, columns=2)
row=table.add_row().cells
p=row[0].add_paragraph('left justified text')
p.alignment=WD_ALIGN_PARAGRAPH.LEFT
p=row[1].add_paragraph('right justified text')
p.alignment=WD_ALIGN_PARAGRAPH.RIGHT

code by: bnlawrence

and to align text to the center just change:

p.alignment=WD_ALIGN_PARAGRAPH.CENTER

solution found here: Modify the alignment of cells in a table

  • So there is no way to do it for the whole table? – minecraftplayer1234 Mar 11 '17 at 17:14
  • 1
    jmh suggested a good solution also, by creating a custom style. check it here: [link](http://stackoverflow.com/questions/28884114/python-docx-align-both-left-and-right-on-same-line) - @Frynio –  Mar 11 '17 at 17:20
3

Well, it seems that adding a paragraph works, but (oh, really?) it addes a new paragraph -- so in my case it wasn't an option. You could change the value of the existing cell and then change paragraph's alignment:

row[0].text = "hey, beauty"
p = row[0].paragraphs[0]
p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER

Actually, in the top answer this first "docx.enum.text" was missing :)

  • This is the best answer when you want to initially set the content of a cell and change its horizontal alignment in one fell swoop. – Mike Finch Sep 11 '22 at 01:44
1

The most reliable way that I have found for setting he alignment of a table cell (or really any text property) is through styles. Define a style for center-aligned text in your document stub, either programatically or through the Word UI. Then it just becomes a matter of applying the style to your text.

If you create the cell by setting its text property, you can just do

for col in table.columns:
    for cell in col.cells:
        cell.paragraphs[0].style = 'My Center Aligned Style'

If you have more advanced contents, you will have to add another loop to your function:

for col in table.columns:
    for cell in col.cells:
        for par in cell.paragraphs:
            par.style = 'My Center Aligned Style'

You can easily stick this code into a function that will accept a table object and a style name, and format the whole thing.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

In my case I used this.

from docx.enum.text import WD_ALIGN_PARAGRAPH

def addCellText(row_cells, index, text):
    row_cells[index].text = str(text)
    paragraph=row_cells[index].paragraphs[0]
    paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT
    font = paragraph.runs[0].font
    font.size= Pt(10)

def addCellTextRight(row_cells, index, text):
    row_cells[index].text = str(text)
    paragraph=row_cells[index].paragraphs[0]
    paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
    font = paragraph.runs[0].font
    font.size= Pt(10)
0

For total alignment to center I use this code:

from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_ALIGN_VERTICAL

for row in table.rows:
    for cell in row.cells:
        cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
        cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
-3

From docx.enum.table import WD_TABLE_ALIGNMENT

table = document.add_table(3, 3)
table.alignment = WD_TABLE_ALIGNMENT.CENTER

For details see a link .

http://python-docx.readthedocs.io/en/latest/api/enum/WdRowAlignment.html

4b0
  • 21,981
  • 30
  • 95
  • 142
CHENG Yan
  • 3
  • 1
  • [Docs for `alignment`](http://python-docx.readthedocs.io/en/latest/api/table.html#docx.table.Table.alignment) say "...specifying the positioning of this table between the page margins". That's not what OP is asking. – Mad Physicist Aug 07 '18 at 17:39