怕搞忘,记下来。
pydocx的表格设置:
1.字体设置
for i in range(len(tab.rows)):
tab.rows[i].height = Cm(2)
for j in range(len(tab.columns)):
tab.cell(i,j).width = Cm(4)
tab.cell(i,j).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
for par in tab.cell(i,j).paragraphs:
print(par.text)
# par.paragraph_format.styles.font.size=Pt(12)
for run in par.runs:
run.font.size = Pt(12)
run.font.name = 'Times New Roman'
run.font.name = u'宋体'
run._element.rPr.rFonts.s
直接用tab.style设置未生效,需要进cell的paragraph对run进行字体设置,可行。
2.段落段落设置
tab.style.paragraph_format.space_before = Pt(5)
tab.style.paragraph_format.space_after = Pt(5)
tab.style.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
tab.style.paragraph_format.line_spacing_rule = WD_LINE_SPACING.SINGLE
3.表格内容居中
https://python-docx.readthedocs.io/en/latest/api/enum/WdCellVerticalAlignment.html
for i in range(len(tab.rows)):
tab.rows[i].height = Cm(2)
for j in range(len(tab.columns)):
tab.cell(i,j).width = Cm(4)
tab.cell(i,j).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
需要在cell中对对起进行设置,有TOP,CENTER,BOTTOM三种,分别对应垂直方向的位置。
4.表格居中
tab.alignment=WD_ALIGN_PARAGRAPH.CENTER
直接对tab使用alignment,设置为居中;
5.表格高度、宽度
高度对ROWS进行设置
for i in range(len(tab.rows)):
tab.rows[i].height = Cm(2)
宽度需要对进到cell对每个cell进行设置,因为每个列中如果有一个cell宽度较大,则按最大的,所以同列中要一样宽。
for i in range(len(tab.rows)):
tab.rows[i].height = Cm(2)
for j in range(len(tab.columns)):
tab.cell(i,j).width = Cm(4)
tab.cell(i,j).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
以上表格应该够了。


被折叠的 条评论
为什么被折叠?



