Python docx add_paragraph() inserts leading newline -
i'm able use paragraph object select font size, color, bold, etc. within table cell. but, add_paragraph()
seems insert leading \n cell , messes formatting on tables.
if use cell.text('')
method doesn't insert newline can't control text attributes.
is there way eliminate leading newline?
here function:
def add_table_cell(table, row, col, text, fontsize=8, r=0, g=0, b=0, width=-1): cell = table.cell(row,col) if (width!=-1): cell.width = inches(width) para = cell.add_paragraph(style=none) para.alignment = wd_align_paragraph.left run = para.add_run(text) run.bold = false run.font.size = pt(fontsize) run.font.color.type == mso_color_type.rgb run.font.color.rgb = rgbcolor(r, g, b)
i tried following , worked out me. not sure if best approach:
cells[0].text = 'some text' #write text cell
#modify paragraph alignment, first paragraph
cells[0].paragraphs[0].paragraph_format.alignment=wd_align_paragraph.center
Comments
Post a Comment