Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the alignment of cells in a table #153

Closed
bnlawrence opened this issue Mar 4, 2015 · 10 comments
Closed

Modify the alignment of cells in a table #153

bnlawrence opened this issue Mar 4, 2015 · 10 comments

Comments

@bnlawrence
Copy link

Hi ...

Would it be possible to add an attribute to table cells so it is possible to set the horizontal alignment (left/centred/right) per cell? I imagine the API would be the same as setting the cell width ... so it would be something like the following to have a left justified first column and right justified columns thereafter (as one might do with a spreadsheet of numbers):

#for say, three columns
alignments=['left','right','right']
for r in table.rows:
    for i in range(len(alignments)):
        r.cells[i].alignment=alignments[i]
@scanny
Copy link
Contributor

scanny commented Mar 4, 2015

I don't believe there is such a setting in Word, Bryan. You can achieve this effect by setting the alignment (justification) of each paragraph in the cell, and you can set each of those separately, perhaps centering the first one and making the rest left justified.

There may be something you can do with table styles though. If you can create a Word document by hand that exhibits the behavior you're after we can take a deeper look.

@bnlawrence
Copy link
Author

Ah thanks. I made the cardinal mistake of mixing a requirement with a possible solution.

The requirement I have is that I can create aligned cells in a table. Following your hint above I can do that simply by aligning the paragraphs in the cells as I create them (which is what I wanted, but I foolishly suggested a way of doing it based on the way I had to handle cell width ... I hadn't appreciated the extra complexity of the paragraphs within the cell ... despite it being pretty obvious if I'd thought about it).

In case anyone else comes here, this is the way of doing it:

# add a row with one cell left aligned, then one right aligned:
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

@scanny
Copy link
Contributor

scanny commented Mar 6, 2015

Glad it worked out Bryan :)

mattjbray added a commit to mattjbray/python-docx that referenced this issue Jul 6, 2015
mattjbray added a commit to mattjbray/python-docx that referenced this issue Jul 6, 2015
@Elditch77
Copy link

This works, however the tabe cells are now much higher, than were before. Any idea how to change that?

@RafaelBraz
Copy link

RafaelBraz commented Jul 9, 2019

For fix an extra \n try this:

table = document.add_table(1, 2)
table.cell(0,0).text = 'Left alignment'
table.cell(0,0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.LEFT
table.cell(0,1).text = 'Right alignment'
table.cell(0,1).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.RIGHT

@Flyzzz
Copy link

Flyzzz commented Aug 3, 2019

get an extra \n,how to fix it?

@yimoxuan
Copy link

yimoxuan commented Jul 7, 2020

get an extra \n,how to fix it?

I solved this problem just now,should not add a new paragraph,just get a origin cell paragraph and set it,like this
cell.text = text cell.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

@rahmanihadi
Copy link

For fix an extra \n try this:

table = document.add_table(1, 2)
table.cell(0,0).text = 'Left alignment'
table.cell(0,0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.LEFT
table.cell(0,1).text = 'Right alignment'
table.cell(0,1).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.RIGHT

Thanks :-)

@henrihapponen
Copy link

If it helps, there is a pretty convenient way to change the style of all cells (or selected ones) of a table like this:
(Note: You have to first create a new style, here called 'My Table Cell Style (Centered)' in Word for whatever format you want: Styles - Create a Style)

for col in table_1.columns:
    for cell in col.cells:
        cell.paragraphs[0].style = 'My Table Cell Style (Centered)'

Or if the cell is more complex (i.e. more paragraphs), then this will do it:

for col in table_1.columns:
    for cell in col.cells:
        for par in cell.paragraphs:
            par.style = 'My Table Cell Style (Centered)'

@vnuness
Copy link

vnuness commented Nov 19, 2021

For fix an extra \n try this:

table = document.add_table(1, 2)
table.cell(0,0).text = 'Left alignment'
table.cell(0,0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.LEFT
table.cell(0,1).text = 'Right alignment'
table.cell(0,1).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.RIGHT

Rafael, amazing solution !! Thank you bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants