Skip to content

Add row to existing table with formatting #205

@drmikecrowe

Description

@drmikecrowe

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions