Skip to content

How to set table cell vertical alignment? #191

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

Closed
zhaozhiming opened this issue Jul 22, 2015 · 4 comments
Closed

How to set table cell vertical alignment? #191

zhaozhiming opened this issue Jul 22, 2015 · 4 comments

Comments

@zhaozhiming
Copy link

HI ALL,
I use table merge cell method to merge some cells to a cell, and I want to set the vertical alignment of the merge cell, but I only find the horizontal alignment set method like this:

table = document.add_table(3, 3)
a = table.cells(0, 0)
b = table.cells(1, 1)
A = a.merge(b)
p = A.add_paragraph("test")
p.alignment = WD_ALIGN_PARAGRAPH.CENTER

How could I set the merge cell vertical alignment to center? Thanks.

@scanny
Copy link
Contributor

scanny commented Jul 25, 2015

That feature isn't implemented yet. At the XML level I think you're looking for the vAlign element, that fits in roughly like this, w:tc being table cell:

<w:tc>
  <w:tcPr>
    <w:vAlign ...>

There is some context analysis here:
http://python-docx.readthedocs.org/en/latest/dev/analysis/features/table-cell.html

You'd need to dig into the schema to work out the specifics of the w:vAlign element or just inspect some Word XML. Then you can get at it with lxml calls from the w:tcPr element. You can get the w:tcPr element with:

tc = cell._tc
tcPr = tc.get_or_add_tcPr()

If you google on "python-docx OxmlElement" you'll find how to use some of the internal python-docx helper functions to get it done. Here's one to get you started:
#55 (comment)

@zhaozhiming
Copy link
Author

OK, thank you very much.

@hmlee9
Copy link

hmlee9 commented Oct 28, 2016

@zhaozhiming @scanny
it should be like

tc = row.cells[0]._tc
tcPr = tc.get_or_add_tcPr()
tcVAlign = OxmlElement('w:vAlign')
tcVAlign.set(qn('w:val'), "center")
tcPr.append(tcVAlign)

@zooyf
Copy link

zooyf commented Nov 25, 2019

        cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

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

4 participants