question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modify the alignment of cells in a table

See original GitHub issue

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]

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
RafaelBrazcommented, Aug 3, 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
4reactions
bnlawrencecommented, Mar 5, 2015

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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Align text in a cell - Microsoft Support
Select the cells that have the text you want aligned. · On the Home tab choose one of the following alignment options: ·...
Read more >
(Legacy) HTML: Tables: Alignment Within a Table
It is possible to change the horizontal alignment of items within table cells. Table data defaults to left alignment; table headers to ...
Read more >
Excel XP: Text and Cell Alignments - GCF Global
To change horizontal alignment using the Format Cells dialog box: · Select a cell or range of cells. · Choose Format > Cells...
Read more >
How to control a Word table's horizontal alignment
Use the Alignment dropdown in the General section. Using the Align options on the menu will align the text within the cells, not...
Read more >
Set Table & Cell Alignment and Change Text Direction
Select cells you want to align. Click the Layout contextual tab. Click an alignment button from the Alignement group (see above figure). Change...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found