python docx 学习笔记 paragraph

paragraph学习笔记

  1. 修改段落格式属性
    修改段落格式使用段落的paragraph_format属性,会生成一个 ParagraphFormat对象
  2. 设置段落对齐方式
document = Document()
p = document.add_paragraph()
pFormat = p.paragraph_format
pFormat.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
#对齐方式包括: left, centered, right, or fully justified
p.add_run('hello,world')
  1. 设置段落缩进方式
#1.段落整体缩进
paragraph_format.left_indent = Inches(0.5)
#2.段落整体右缩进
paragraph_format.left_indent = Pt(24)
#3.段落首行缩进用first_line_indent属性指定
pFormat.first_line_indent = Pt(24)
  1. 设置段落前后间距
#1.段前间距
paragraph_format.space_before = Pt(18)
#2.段后间距
paragraph_format.space_after = Pt(12)
  1. 设置行间距
#设置行间距可以设置为绝对值和相对值(相对于字体大小)
#所用到的属性为line_spacing 和  line_spacing_rule(枚举类型)
#line_spacing_rule包括:SINGLE, DOUBLE, or ONE_POINT_FIVE
#line_spacing为整数时,表示绝对值,为浮点数时,表示相对值
paragraph_format.line_spacing = 1.75
#line_spacing_rule的值为WD_LINE_SPACING的属性
paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
  1. 设置分页
#设置分页使用属性keep_together, keep_with_next, page_break_before, and widow_control

posted on 2022-05-23 16:30  朝朝暮Mu  阅读(725)  评论(0)    收藏  举报