-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
I have some code that outputs the below table.
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:
- 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. - 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. - 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)`