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.

Help removing leading blank line before paragraph.run text in table cells

See original GitHub issue

I have some code that outputs the below table.

tableexample

The table contains data that is added using: add_paragraph().add_run('Some text here...')

The issue I am having is that each cell contains a leading blank line before text is inserted.

I have done some research and managed to find a few different approaches none of which have managed to resolve my issue.

_Among the things I have tried is:

  1. Setting space_before and space_after to PT(0) Also found this post https://github.com/python-openxml/python-docx/issues/305 by @scanny and was able to print the xml values and they are set to zero both before and after.
  2. Using the cell.text method does not include the leading blank line but removes the ability to control formatting such as bolding text. I tried different variations of adding the cell.text = ‘’ and then setting paragraph[0] to a run and then trying to set formatting but this either ended with no text being written to the table at all or none of the formatting such as bolding being recognized.
  3. Also made an attempt at parsing the XML and using regex to set xml:space = preserve to default but this failed hard._

I would greatly appreciate any assistance anyone can offer with removing the leading blank lines.

The code that draws the table is below:

`document = report document.add_page_break() table = document.add_table(rows=14, cols=4) style = document.styles[‘Normal’] font = style.font table.width = ‘100%’ table.style = document.styles[‘GridTable4-Accent3’]

def set_column_width(column, width): column.width = width for cell in column.cells: cell.width = width set_column_width(table.columns[0], Inches(1.25)) set_column_width(table.columns[1], Inches(0.5)) set_column_width(table.columns[2], Inches(0.5)) set_column_width(table.columns[3], Inches(4.75))

font.name = ‘Calibri’ font.size = Pt(11) font.bold = False row = table.rows[2] row.height = Inches(0.75)

a = table.cell(0, 0) b = table.cell(0, 3) a.merge(b)

x = 3 while x < 14: a = table.cell(x, 0) b = table.cell(x, 3) a.merge(b) x = x + 1

if fields[2] == ‘WWWWW’: cell0color = parse_xml(r’<w:shd {} w:fill=“800000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[0]._tc.get_or_add_tcPr().append(cell0color) cell1color = parse_xml(r’<w:shd {} w:fill=“800000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[1]._tc.get_or_add_tcPr().append(cell1color) cell2color = parse_xml(r’<w:shd {} w:fill=“800000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[2]._tc.get_or_add_tcPr().append(cell2color) elif fields[2] == ‘XXXXX’: cell0color = parse_xml(r’<w:shd {} w:fill=“ff0000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[0]._tc.get_or_add_tcPr().append(cell0color) cell1color = parse_xml(r’<w:shd {} w:fill=“ff0000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[1]._tc.get_or_add_tcPr().append(cell1color) cell2color = parse_xml(r’<w:shd {} w:fill=“ff0000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[2]._tc.get_or_add_tcPr().append(cell2color) elif fields[2] == ‘YYYYY’: cell0color = parse_xml(r’<w:shd {} w:fill=“ffc000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[0]._tc.get_or_add_tcPr().append(cell0color) cell1color = parse_xml(r’<w:shd {} w:fill=“ffc000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[1]._tc.get_or_add_tcPr().append(cell1color) cell2color = parse_xml(r’<w:shd {} w:fill=“ffc000”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[2]._tc.get_or_add_tcPr().append(cell2color) elif fields[2] == ‘ZZZZZ’: cell0color = parse_xml(r’<w:shd {} w:fill=“00b050”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[0]._tc.get_or_add_tcPr().append(cell0color) cell1color = parse_xml(r’<w:shd {} w:fill=“00b050”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[1]._tc.get_or_add_tcPr().append(cell1color) cell2color = parse_xml(r’<w:shd {} w:fill=“00b050”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[2]._tc.get_or_add_tcPr().append(cell2color) else: cell0color = parse_xml(r’<w:shd {} w:fill=“00bfff”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[0]._tc.get_or_add_tcPr().append(cell0color) cell1color = parse_xml(r’<w:shd {} w:fill=“00bfff”/>‘.format(nsdecls(‘w’))) table.rows[2].cells[1]._tc.get_or_add_tcPr().append(cell1color) cell2color = parse_xml(r’<w:shd {} w:fill=“00bfff”/>'.format(nsdecls(‘w’))) table.rows[2].cells[2]._tc.get_or_add_tcPr().append(cell2color)

for table in report.tables: for row in table.rows: for cell in row.cells: for paragraph in cell.paragraphs: paragraph_format = paragraph.paragraph_format paragraph_format.space_before = Pt(0) paragraph_format.space_after = Pt(0)

row = table.rows[1].cells p = row[0].add_paragraph().add_run(‘Minor Header 1’) font = p.font font.small_caps = True font.size = Pt(10) font.bold = True font.color.rgb = RGBColor(204, 204, 204) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Inches(0) p = row[1].add_paragraph().add_run(‘MH 2’) font = p.font font.small_caps = True font.size = Pt(10) font.bold = True font.color.rgb = RGBColor(204, 204, 204) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0) p = row[2].add_paragraph().add_run(‘MH 3’) font = p.font font.small_caps = True font.size = Pt(10) font.bold = True font.color.rgb = RGBColor(204, 204, 204) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0) p = row[3].add_paragraph().add_run(‘Minor Header 4’) font = p.font font.small_caps = True font.size = Pt(10) font.bold = True font.color.rgb = RGBColor(204, 204, 204) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0)

row = table.rows[2].cells p = row[0].add_paragraph().add_run(‘Minor Header 1’) font = p.font font.size = Pt(12.5) font.bold = True font.color.rgb = RGBColor(255, 255, 255) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0) p = row[1].add_paragraph().add_run(‘MH 2’) font = p.font font.size = Pt(12.5) font.bold = True font.color.rgb = RGBColor(255, 255, 255) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0) p = row[2].add_paragraph().add_run(MH 3) font = p.font font.size = Pt(12.5) font.bold = True font.color.rgb = RGBColor(255, 255, 255) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0) p = row[3].add_paragraph().add_run(‘Minor Header 4’) p.base_style = ‘Heading2’ font = p.font font.size = Pt(12.5) font.bold = True font.color.rgb = RGBColor(0, 0, 0) p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER p.space_before = Pt(0)

row = table.rows[3].cells p = row[0].add_paragraph().add_run(‘Section Header’) font = p.font font.bold = True p.space_before = Pt(0) row = table.rows[4].cells p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False row = table.rows[5].cells p = row[0].add_paragraph().add_run(‘Section Header’) font = p.font font.bold = True p.space_before = Pt(0) row = table.rows[6].cells p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False row = table.rows[7].cells p = row[0].add_paragraph().add_run(‘Section Header’) font = p.font font.bold = True p.space_before = Pt(0) row = table.rows[8].cells p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False row = table.rows[9].cells p = row[0].add_paragraph().add_run(‘Section Header’) font = p.font font.bold = True p.space_before = Pt(0) row = table.rows[10].cells p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False row = table.rows[11].cells p = row[0].add_paragraph().add_run(‘Section Header’) font = p.font font.bold = True p.space_before = Pt(0) row = table.rows[12].cells p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False p = row[0].add_paragraph().add_run() p.add_picture(‘Section Data’) p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False p = row[0].add_paragraph().add_run() p.add_picture(‘Section Data’) p = row[0].add_paragraph().add_run(‘Section Data’) font = p.font font.bold = False p = row[0].add_paragraph().add_run(‘Section Data’) p.add_picture((‘Section Data’) p = row[0].add_paragraph().add_run(’ ') font = p.font font.bold = False row = table.rows[13] row.cells[0].text = ‘’

for table in report.tables: for row in table.rows: for cell in row.cells: for paragraph in cell.paragraphs: # paragraph_format = document.styles[‘GridTable4-Accent3’].paragraph_format paragraph_format = paragraph.paragraph_format paragraph_format.space_before = Pt(0) paragraph_format.space_after = Pt(0)

document.save(report)`

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
drkpasngrcommented, Jul 25, 2018

That worked! Really appreciate the help. Needed a bit of tweaking to still allow controlling formatting but leading blank lines no longer show up.

Code now looks like this:

row = table.rows[1].cells
para_text = 'Section Header'
cell = row[0]
pt = cell.paragraphs[0]
t = pt.text = ''
p = pt.add_run(para_text)
font = p.font
font.bold = True
p.space_before = Pt(0)
2reactions
scannycommented, Jul 25, 2018

Unfortunately adding paragraphs to a table cell is not symmetric. A “new” table cell contains a single paragraph. Per the specification, it can not be completely empty. Consequently, the first paragraph must be populated in a different way then the second and later paragraphs; something like this:

para_text = sequence_of_lines_each_representing_a_paragraph = ('Foo', 'Bar', ...)
cell.paragraphs[0].text = para_text[0]
for text in para_text[1:]:
    cell.add_paragraph(text)
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Removing first line in table cell with docx package
A "new" table cell already contains a single empty paragraph. The first paragraph must be populated in a different way then the second...
Read more >
MS Word How to Remove Empty Extra Lines & How to Joint ...
Remove unwanted empty line in MS Word Document #EmptyLines #Space #MSWordFacebook : https://www.facebook.com/mjtube11Follow on Instagram ...
Read more >
Remove Blank Paragraphs at Start and at the End of a Cell
I have a Tables in a Document, each with 2 Columns. The heading row will always contain the same Strings ("#" and “Req”),...
Read more >
how to remove an empty line before a table on header
Hi All, I am trying to insert a table in a Word document header, but I don't know why I cannot remove the...
Read more >
3 Effective Ways to Remove Unwanted Spaces in Table Cells ...
Method 1: Narrow Down the Row Height Manually · First and foremost, check if there are removable blank lines in cells. · Then...
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