python-docx包之----设置段落样式(缩进/对齐/间距)

需求:

之前学会了简单的设置行run的样式,现在有些更高要求,需要对段落设置样式,这里就用到了docx包。

1. 缩进(首行缩进,左/右缩进)

2. 对齐

3. 间距

4. 在word中指定格式,不需程序专门设定

python-docx包之----设置段落样式(缩进/对齐/间距)_第1张图片

实现一:缩进

缩进包括:首行缩进(first_line_indent),左/右缩进(left_indent/right_indent)

缩进的时候需要设置单位,Pt  Cm  Inches

from docx.shared import Pt, Cm, Inches

A 首行缩进

1. 模板

{{p mytest }}

2. 代码

\n换行可以识别

from docxtpl import DocxTemplate
from docx import *
from docx.shared import Cm

def generate_word():
    tpl=DocxTemplate('template.docx')
    
    sd = tpl.new_subdoc()
    p = sd.add_paragraph()
   

你可能感兴趣的:(python-docx包之----设置段落样式(缩进/对齐/间距))