Add row to existing table with formatting
See original GitHub issueHow do you add a row to an existing table that you’ve opened via Document(‘filename.docx’), for example, and match the table formatting?
I’ve tried things like:
last_row = table.rows[-1]
for item in data:
try:
row_cells = table.add_row()
row_cells.cells[0].text = item['label']
row_cells.cells[1].text = item['description']
row_cells.cells[0].paragraphs[0].style = 'MediumGrid1-Accent21'
# and
row_cells.cells[0].paragraphs[0].style = last_row.cells[0].paragraphs[0].style
I can add the row without problems, but I can’t match the formatting of the row above.
Ideally, would love something like:
last_row = table.rows[-1]
for item in data:
try:
table.rows.push(last_row)
row_cells = table.rows[-1]
row_cells.cells[0].text = item['label']
row_cells.cells[1].text = item['description']
So, have the ability to append the prior row to the end, then change the internal text. Is that feasible?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:8
- Comments:5
Top Results From Across the Web
Unable to add rows in Excel 2007 after 'Format as table' - MSDN
Hi. The problem for me was that I had used the 'format as table' option in my spreadsheet, to alternate row colors.
Read more >Excel table not formatting new rows appropriately
Click the table which you want to be main format table. · click design. · Go to "Properties" · Click "Resize table" ·...
Read more >How to Add a Row or Column to a Table in Excel
Select a cell in the table row or column next to where you want to add the row or column. Insert options aren't...
Read more >Inserting Rows into Tables of Data
Right-click on the row number on the left side and select Insert. This inserts a row all the way across the worksheet, preserving...
Read more >How to Add New Row Automatically in an Excel Table
Press Alt+I on your keyboard. Then press R. It will insert a new row, like above.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

doc= Document(‘…path of your document…’)--------->loads the document table=doc.tables[0]---------> gets you the first table in the document row=table.add_row().cells--------------->adds row at the end of the table with same format row[0].text = item[‘label’]---------> adds data to the last added row and the 0th column, the first column
There seem to be a few properties that python-docx doesn’t expose or handle. This is what one of the already existing table rows looks like:
And this is what a newly added table row looks like:
I think many of the attributes are not important, but some like
<w:shd>definitely are. I also have no clue where the font size is coming from - all existing table rows have font size 9, but the style has font size 12 🤨 maybe because of adjusting text to cell sizes?For reference, here is also the table header: