python-docx设置word表格内容、字体、间距、对齐

def set_font(cell):
    cell.paragraphs[0].runs[0].font.name = "Times New Roman" #设置英文字体
    cell.paragraphs[0].runs[0].font.size = Pt(9) # 字体大小
    cell.paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体') #设置中文字体


for table in tables:
    for row in range(len(table.rows)):
        for column in range(len(table.columns)):
            table.cell(row, column).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.LEFT #对齐
            table.cell(row, column).paragraphs[0].paragraph_format.line_spacing = 1 #段落行间距
            set_font(table.cell(row, column)) 

你可能感兴趣的:(python,word,开发语言)