Removing first line in table cell with docx package

Viewed 550

I'm new to docx and am trying to remove the first line when adding a new paragraph to a table cell.

from docx import Document
from docx.shared import Pt

document.add_heading().add_run('HEADING').font.name = 'Calibri'
a = document.add_table(1, 2, style= 'Table Grid')
a_cells = a.rows[0].cells

a1 = a_cells[0].add_paragraph()
a1 = a1.add_run('NAME').font
a1.bold = True
a1.name = 'Calibri'
a1.size = Pt(14)

NAME will be printed on the second line. I can use:

a1 = a_cells[0].text = 'NAME'

instead of add_paragraph but I can't add bold format.

So is there a way to either; remove the first line of add_paragraph or format .text to bold?

1 Answers

A "new" table cell already contains a single empty paragraph. The first paragraph must be populated in a different way then the second and later paragraphs; something like this:

cell = table.rows[0].cells[0]
paragraph = cell.paragraphs[0]
run = paragraph.add_run('NAME')
font = run.font
font.bold = True
font.name = 'Calibri'
font.size = Pt(14)

second_paragraph = cell.add_paragraph(...)
Related