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.

Add row to existing table with formatting

See original GitHub issue

How 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:open
  • Created 8 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

3reactions
AbhishekTiwari-342123commented, Jun 9, 2021

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

0reactions
ndahncommented, Feb 14, 2022

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:

  <w:tr w:rsidR="007B0145" w:rsidRPr="007611D4" w14:paraId="02B34162" w14:textId="77777777" w:rsidTr="00B818C1">
    <w:tc>
      <w:tcPr>
        <w:tcW w:w="2727" w:type="dxa"/>
        <w:shd w:val="pct5" w:color="000000" w:fill="FFFFFF"/>
      </w:tcPr>
      <w:p w14:paraId="4F51D0BC" w14:textId="77777777" w:rsidR="007B0145" w:rsidRPr="007611D4" w:rsidRDefault="007B0145" w:rsidP="00B818C1">
        <w:pPr>
          <w:rPr>
            <w:sz w:val="18"/>
            <w:szCs w:val="18"/>
            <w:lang w:val="en-US"/>
          </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="007611D4">
          <w:rPr>
            <w:sz w:val="18"/>
            <w:szCs w:val="18"/>
            <w:lang w:val="en-US"/>
          </w:rPr>
          <w:t>Total</w:t>
        </w:r>
      </w:p>
    </w:tc>
  </w:tr>

And this is what a newly added table row looks like:

  <w:tr>
    <w:tc>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="2727"/>
      </w:tcPr>
      <w:p>
        <w:r>
          <w:t>Kat1+2+6</w:t>
        </w:r>
      </w:p>
    </w:tc>
  </w:tr>

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:

  <w:tbl>
    <w:tblPr>
      <w:tblW w:w="9531" w:type="dxa"/>
      <w:tblInd w:w="108" w:type="dxa"/>
      <w:tblBorders>
        <w:insideH w:val="single" w:sz="18" w:space="0" w:color="FFFFFF"/>
        <w:insideV w:val="single" w:sz="18" w:space="0" w:color="FFFFFF"/>
      </w:tblBorders>
      <w:tblLayout w:type="fixed"/>
      <w:tblLook w:val="01E0" w:firstRow="1" w:lastRow="1" w:firstColumn="1" w:lastColumn="1" w:noHBand="0" w:noVBand="0"/>
    </w:tblPr>
    <w:tblGrid>
      <w:gridCol w:w="2727"/>
      <w:gridCol w:w="1134"/>
      <w:gridCol w:w="1276"/>
      <w:gridCol w:w="992"/>
      <w:gridCol w:w="1276"/>
      <w:gridCol w:w="992"/>
      <w:gridCol w:w="1134"/>
    </w:tblGrid>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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