段落格式

WordprocessingML支持多种段落格式属性来控制布局特征,如对齐、缩进、行距、前后空格和窗口/孤立控件。

对齐(对齐)

在Word中,每个段落都有一个 alignment 属性,该属性指定当段落在页面上布局时如何对齐段落的各行。常见的值有左、右、居中和对齐。

协议

获取和设置段落对齐方式:

>>> paragraph = body.add_paragraph()
>>> paragraph.alignment
None
>>> paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
>>> paragraph.alignment
RIGHT (2)
>>> paragraph.alignment = None
>>> paragraph.alignment
None

XML语义

如果 <w:jc> 元素不在段落上,则该段落的对齐值从其样式层次结构继承。如果元素存在,其值将覆盖任何继承的值。从API中,值为 NoneParagraph.alignment 属性对应于“否” <w:jc> 元素存在。如果 None 分配给 Paragraph.alignment , the <w:jc> 元素已删除。

段落间距

后续段落之间的间距由段落间距属性控制。可以在段落之前或之后应用间距,也可以同时应用这两种间距。这一概念类似于 paddingmargin 在css中。WordProcingML支持将段落间距指定为长度值或行高的倍数;但是,通过Word UI仅支持长度值。段落之间的间距是重叠的,因此两个段落之间呈现的间距是第一个段落之后的空格和第二个段落之前的空格的最大值。

协议

获取和设置段落间距:

>>> paragraph_format = document.styles['Normal'].paragraph_format
>>> paragraph_format.space_before
None
>>> paragraph_format.space_before = Pt(12)
>>> paragraph_format.space_before.pt
12.0

XML语义

  • 段落间距使用 w:pPr/w:spacing 元素,它还控制行距。间距以twips为单位指定。

  • 如果 w:spacing 元素不存在,段落间距是从样式层次结构继承的。

  • 如果不在样式层次结构中出现,则段落将没有间距。

  • 如果 w:spacing 元素存在,但特定属性(例如。 w:before )不是,它的值是继承的。

XML样本

前12位空格,后0位:

<w:pPr>
  <w:spacing w:before="240" w:after="0"/>
</w:pPr>

行距

行距可以指定为特定长度,也可以指定为行高(字体大小)的倍数。行距由中的值组合指定 w:spacing/@w:linew:spacing/@w:lineRule . 这个 ParagraphFormat.line_spacing 属性根据赋值是否为的实例确定要使用的方法 Length .

协议

获取和设置行距:

>>> paragraph_format.line_spacing, paragraph_format.line_spacing_rule
(None, None)

>>> paragraph_format.line_spacing = Pt(18)
>>> paragraph_format.line_spacing, paragraph_format.line_spacing_rule
(228600, WD_LINE_SPACING.EXACTLY (4))

>>> paragraph_format.line_spacing = 1
>>> paragraph_format.line_spacing, paragraph_format.line_spacing_rule
(152400, WD_LINE_SPACING.SINGLE (0))

>>> paragraph_format.line_spacing = 0.9
>>> paragraph_format.line_spacing, paragraph_format.line_spacing_rule
(137160, WD_LINE_SPACING.MULTIPLE (5))

XML语义

  • 行距由中的值的组合指定 w:spacing/@w:linew:spacing/@w:lineRule .

  • w:spacing/@w:line 以twips指定。如果 @w:lineRule 是“自动”(或缺失), @w:line 被解释为一条线的240分之一。对于所有其他值 @w:lineRule 的价值 @w:line 被解释为以twips为单位的特定长度。

  • 如果 w:spacing 元素不存在,行距被继承。

  • 如果 @w:line 不存在,行距被继承。

  • 如果不存在, @w:lineRule 默认为“自动”。

  • 如果样式层次结构中不存在行距,则行距默认为单倍行距。

  • “至少”的值 @w:lineRule 表示行距为 @w:line twips或单间隔,以较大者为准。

XML样本

14分:

<w:pPr>
  <w:spacing w:line="280"/>
</w:pPr>

双倍行距:

<w:pPr>
  <w:spacing w:line="480" w:lineRule="exact"/>
</w:pPr>

缩进

段落缩进是使用 w:pPr/w:ind 元素。可以指定左、右、第一行和悬挂缩进。缩进可以指定为长度或字符宽度的百分之一。只支持长度 python-docx . 第一行缩进和挂起缩进都使用 ParagraphFormat.first_line_indent 属性。指定正值将产生缩进的第一行。负值产生挂起的缩进。

协议

获取和设置缩进:

>>> paragraph_format.left_indent
None
>>> paragraph_format.right_indent
None
>>> paragraph_format.first_line_indent
None

>>> paragraph_format.left_indent = Pt(36)
>>> paragraph_format.left_indent.pt
36.0

>>> paragraph_format.right_indent = Inches(0.25)
>>> paragraph_format.right_indent.pt
18.0

>>> paragraph_format.first_line_indent = Pt(-18)
>>> paragraph_format.first_line_indent.pt
-18.0

XML语义

  • 缩进由指定 w:ind/@w:startw:ind/@w:endw:ind/@w:firstLinew:ind/@w:hanging .

  • w:firstLinew:hanging 是互斥的,如果两者都指定了, w:firstLine 被忽略。

  • 所有四个属性都在twips中指定。

  • w:start 控制从左到右段落的左缩进或从右到左段落的右缩进。 w:end 控制另一边。如果指定了镜像缩进, w:start 控制内边距和 w:end 外面。允许负值,并导致文本移过文本边距。

  • 如果 w:ind 不存在,缩进被继承。

  • 任何省略的属性都将被继承。

  • 如果样式层次结构中不存在,则缩进值默认为零。

XML样本

左1英寸,0.5英寸(附加)第一行,0.5英寸右侧:

<w:pPr>
  <w:ind w:start="1440" w:end="720" w:firstLine="720"/>
</w:pPr>

左0.5英寸,0.5英寸悬挂缩进:

<w:pPr>
  <w:ind w:start="720" w:hanging="720"/>
</w:pPr>

页面放置

有一些页面放置属性可以控制这样的事情,比如将一个段落的行放在同一页上,将一个段落(例如标题)保持在与下一个段落相同的页面上,以及将段落放在新页面的顶部。这些都是三态布尔属性,其中 None 表示“继承”。

协议

获取和设置缩进:

>>> paragraph_format.keep_with_next
None
>>> paragraph_format.keep_together
None
>>> paragraph_format.page_break_before
None
>>> paragraph_format.widow_control
None

>>> paragraph_format.keep_with_next = True
>>> paragraph_format.keep_with_next
True

>>> paragraph_format.keep_together = False
>>> paragraph_format.keep_together
False

>>> paragraph_format.page_break_before = True
>>> paragraph_format.widow_control = None

XML语义

  • 所有四个元素都有“On/Off”语义。

  • 如果不存在,则其值将被继承。

  • 如果样式层次结构中不存在,则值默认为False。

XML样本

“与下一个保持”、“保持在一起”、“之前没有分页符”和“寡妇/孤儿控件”:

<w:pPr>
  <w:keepNext/>
  <w:keepLines/>
  <w:pageBreakBefore w:val="0"/>
  <w:widowControl/>
</w:pPr>

枚举

XML样本

具有继承对齐方式的段落:

<w:p>
  <w:r>
    <w:t>Inherited paragraph alignment.</w:t>
  </w:r>
</w:p>

右对齐段落:

<w:p>
  <w:pPr>
    <w:jc w:val="right"/>
  </w:pPr>
  <w:r>
    <w:t>Right-aligned paragraph.</w:t>
  </w:r>
</w:p>

架构摘要

<xsd:complexType name="CT_PPr">  <!-- denormalized -->
  <xsd:sequence>
    <xsd:element name="pStyle"              type="CT_String"           minOccurs="0"/>
    <xsd:element name="keepNext"            type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="keepLines"           type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="pageBreakBefore"     type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="framePr"             type="CT_FramePr"          minOccurs="0"/>
    <xsd:element name="widowControl"        type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="numPr"               type="CT_NumPr"            minOccurs="0"/>
    <xsd:element name="suppressLineNumbers" type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="pBdr"                type="CT_PBdr"             minOccurs="0"/>
    <xsd:element name="shd"                 type="CT_Shd"              minOccurs="0"/>
    <xsd:element name="tabs"                type="CT_Tabs"             minOccurs="0"/>
    <xsd:element name="suppressAutoHyphens" type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="kinsoku"             type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="wordWrap"            type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="overflowPunct"       type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="topLinePunct"        type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="autoSpaceDE"         type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="autoSpaceDN"         type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="bidi"                type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="adjustRightInd"      type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="snapToGrid"          type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="spacing"             type="CT_Spacing"          minOccurs="0"/>
    <xsd:element name="ind"                 type="CT_Ind"              minOccurs="0"/>
    <xsd:element name="contextualSpacing"   type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="mirrorIndents"       type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="suppressOverlap"     type="CT_OnOff"            minOccurs="0"/>
    <xsd:element name="jc"                  type="CT_Jc"               minOccurs="0"/>
    <xsd:element name="textDirection"       type="CT_TextDirection"    minOccurs="0"/>
    <xsd:element name="textAlignment"       type="CT_TextAlignment"    minOccurs="0"/>
    <xsd:element name="textboxTightWrap"    type="CT_TextboxTightWrap" minOccurs="0"/>
    <xsd:element name="outlineLvl"          type="CT_DecimalNumber"    minOccurs="0"/>
    <xsd:element name="divId"               type="CT_DecimalNumber"    minOccurs="0"/>
    <xsd:element name="cnfStyle"            type="CT_Cnf"              minOccurs="0"/>
    <xsd:element name="rPr"                 type="CT_ParaRPr"          minOccurs="0"/>
    <xsd:element name="sectPr"              type="CT_SectPr"           minOccurs="0"/>
    <xsd:element name="pPrChange"           type="CT_PPrChange"        minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CT_FramePr">
  <xsd:attribute name="dropCap"    type="ST_DropCap"/>
  <xsd:attribute name="lines"      type="ST_DecimalNumber"/>
  <xsd:attribute name="w"          type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="h"          type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="vSpace"     type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="hSpace"     type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="wrap"       type="ST_Wrap"/>
  <xsd:attribute name="hAnchor"    type="ST_HAnchor"/>
  <xsd:attribute name="vAnchor"    type="ST_VAnchor"/>
  <xsd:attribute name="x"          type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="xAlign"     type="s:ST_XAlign"/>
  <xsd:attribute name="y"          type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="yAlign"     type="s:ST_YAlign"/>
  <xsd:attribute name="hRule"      type="ST_HeightRule"/>
  <xsd:attribute name="anchorLock" type="s:ST_OnOff"/>
</xsd:complexType>

<xsd:complexType name="CT_Ind">
  <xsd:attribute name="start"          type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="startChars"     type="ST_DecimalNumber"/>
  <xsd:attribute name="end"            type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="endChars"       type="ST_DecimalNumber"/>
  <xsd:attribute name="left"           type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="leftChars"      type="ST_DecimalNumber"/>
  <xsd:attribute name="right"          type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="rightChars"     type="ST_DecimalNumber"/>
  <xsd:attribute name="hanging"        type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="hangingChars"   type="ST_DecimalNumber"/>
  <xsd:attribute name="firstLine"      type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="firstLineChars" type="ST_DecimalNumber"/>
</xsd:complexType>

<xsd:complexType name="CT_Jc">
  <xsd:attribute name="val" type="ST_Jc" use="required"/>
</xsd:complexType>

<xsd:complexType name="CT_OnOff">
  <xsd:attribute name="val" type="s:ST_OnOff"/>
</xsd:complexType>

<xsd:complexType name="CT_Spacing">
  <xsd:attribute name="before"            type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="beforeLines"       type="ST_DecimalNumber"/>
  <xsd:attribute name="beforeAutospacing" type="s:ST_OnOff"/>
  <xsd:attribute name="after"             type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="afterLines"        type="ST_DecimalNumber"/>
  <xsd:attribute name="afterAutospacing"  type="s:ST_OnOff"/>
  <xsd:attribute name="line"              type="ST_SignedTwipsMeasure"/>
  <xsd:attribute name="lineRule"          type="ST_LineSpacingRule"/>
</xsd:complexType>

<xsd:complexType name="CT_String">
  <xsd:attribute name="val" type="s:ST_String" use="required"/>
</xsd:complexType>

<xsd:complexType name="CT_Tabs">
  <xsd:sequence>
    <xsd:element name="tab" type="CT_TabStop" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

<!-- simple types -->

<xsd:simpleType name="ST_Jc">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="start"/>
    <xsd:enumeration value="center"/>
    <xsd:enumeration value="end"/>
    <xsd:enumeration value="both"/>
    <xsd:enumeration value="mediumKashida"/>
    <xsd:enumeration value="distribute"/>
    <xsd:enumeration value="numTab"/>
    <xsd:enumeration value="highKashida"/>
    <xsd:enumeration value="lowKashida"/>
    <xsd:enumeration value="thaiDistribute"/>
    <xsd:enumeration value="left"/>
    <xsd:enumeration value="right"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_LineSpacingRule">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="auto"/>  <!-- default -->
    <xsd:enumeration value="exact"/>
    <xsd:enumeration value="atLeast"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_OnOff">
  <xsd:union memberTypes="xsd:boolean ST_OnOff1"/>
</xsd:simpleType>

<xsd:simpleType name="ST_OnOff1">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="on"/>
    <xsd:enumeration value="off"/>
  </xsd:restriction>
</xsd:simpleType>