我可以使用段落对象来选择表格单元格中的字体大小、颜色、粗体等。但是,add_paragraph()似乎总是在单元格中插入前导\n,这会打乱某些表格的格式。
如果我只是使用cell.text('')方法,它不会插入这个换行符,但是我就不能控制文本属性了。
有没有办法去掉这个前导换行符?
下面是我的函数:
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)发布于 2015-04-26 02:46:32
我尝试了下面的方法,它对我很有效。不确定是否是最好的方法:
cells[0].text = 'Some text' #Write the text to the cell
#Modify the paragraph alignment, first paragraph cells[0].paragraphs[0].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.CENTER
发布于 2016-04-05 01:46:59
我找到的解决方案是使用文本属性而不是add_paragraph(),而不是使用add_run():
row_cells[0].text = ''
row_cells[0].paragraphs[0].add_run('Total').bold = True
row_cells[0].paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT发布于 2020-11-19 02:33:09
以下是对我起作用的方法。我不调用add_paragraph()。我只用这个调用-> para = cell.paragraphs引用了第一段。在此之后的所有内容都是常规的api调用。
table = doc.add_table( rows=1, cols=3 ) # bar codes
for tableRow in table.rows:
for cell in tableRow.cells:
para = cell.paragraphs[0]
run = para.add_run( "*" + specIDStr + "*" )
font = run.font
font.name = 'Free 3 of 9'
font.size = Pt( 20 )
run = para.add_run( "\n" + specIDStr
+ "\n" + firstName + " " + lastName
+ "\tDOB: " + dob )
font = run.font
font.name = 'Arial'
font.size = Pt( 8 )https://stackoverflow.com/questions/29379348
复制相似问题