表属性

对准

Word允许表格在页边距之间左对齐、右对齐或居中对齐。

读/写 Table.alignment 属性指定表的对齐方式:

>>> table = document.add_table(rows=2, cols=2)
>>> table.alignment
None
>>> table.alignment = WD_TABLE_ALIGNMENT.RIGHT
>>> table.alignment
RIGHT (2)

自动调整

Word有两种布局表格的算法, fixed-widthautofit 。默认设置为自动适应。Word将根据单元格内容调整自动调整表格中的列宽。无论内容如何,固定宽度的表格都会保持其列宽。当总表格宽度超过页面宽度时,这两种算法都会按比例调整列宽。

读/写 Table.allow_autofit 属性指定使用哪种算法::

>>> table = document.add_table(rows=2, cols=2)
>>> table.allow_autofit
True
>>> table.allow_autofit = False
>>> table.allow_autofit
False

XML样本

以下XML表示2x2表:

<w:tbl>
  <w:tblPr>
    <w:tblStyle w:val="TableGrid"/>
    <w:tblW w:type="auto" w:w="0"/>
    <w:jc w:val="right"/>
    <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0"
               w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
  </w:tblPr>
  <w:tblGrid>
    <w:gridCol w:w="4788"/>
    <w:gridCol w:w="4788"/>
  </w:tblGrid>
  <w:tr>
    <w:tc/>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="4788"/>
      </w:tcPr>
      <w:p/>
    </w:tc>
    <w:tc>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="4788"/>
      </w:tcPr>
      <w:p/>
    </w:tc>
  </w:tr>
  <w:tr>
    <w:tc>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="4788"/>
      </w:tcPr>
      <w:p/>
    </w:tc>
    <w:tc>
      <w:tcPr>
        <w:tcW w:type="dxa" w:w="4788"/>
      </w:tcPr>
      <w:p/>
    </w:tc>
  </w:tr>
</w:tbl>

布局行为

自动布局会导致实际列宽不可预测且不稳定。对内容的更改会使表格布局发生变化。

CT TblWidth元素的语义

例如 tcW ::

<w:tcW w:w="42.4mm"/>

<w:tcW w:w="1800" w:type="dxa"/>

<w:tcW w:w="20%" w:type="pct"/>

<w:tcW w:w="0" w:type="auto"/>

<w:tcW w:type="nil"/>


ST_MeasurementOrPercent
|
+-- ST_DecimalNumberOrPercent
|   |
|   +-- ST_UnqualifiedPercentage
|   |   |
|   |   +-- XsdInteger e.g. '1440'
|   |
|   +-- ST_Percentage e.g. '-07.43%'
|
+-- ST_UniversalMeasure  e.g. '-04.34mm'

架构定义

<xsd:complexType name="CT_Tbl">  <!-- denormalized -->
  <xsd:sequence>
    <xsd:group    ref="EG_RangeMarkupElements"        minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element name="tblPr"       type="CT_TblPr"/>
    <xsd:element name="tblGrid"     type="CT_TblGrid"/>
    <xsd:choice                                       minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="tr"        type="CT_Row"/>
      <xsd:element name="customXml" type="CT_CustomXmlRow"/>
      <xsd:element name="sdt"       type="CT_SdtRow"/>
      <xsd:group    ref="EG_RunLevelElts"             minOccurs="0" maxOccurs="unbounded"/>
    </xsd:choice>
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CT_TblPr">  <!-- denormalized -->
  <xsd:sequence>
    <xsd:element name="tblStyle"            type="CT_String"        minOccurs="0"/>
    <xsd:element name="tblpPr"              type="CT_TblPPr"        minOccurs="0"/>
    <xsd:element name="tblOverlap"          type="CT_TblOverlap"    minOccurs="0"/>
    <xsd:element name="bidiVisual"          type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="tblStyleRowBandSize" type="CT_DecimalNumber" minOccurs="0"/>
    <xsd:element name="tblStyleColBandSize" type="CT_DecimalNumber" minOccurs="0"/>
    <xsd:element name="tblW"                type="CT_TblWidth"      minOccurs="0"/>
    <xsd:element name="jc"                  type="CT_JcTable"       minOccurs="0"/>
    <xsd:element name="tblCellSpacing"      type="CT_TblWidth"      minOccurs="0"/>
    <xsd:element name="tblInd"              type="CT_TblWidth"      minOccurs="0"/>
    <xsd:element name="tblBorders"          type="CT_TblBorders"    minOccurs="0"/>
    <xsd:element name="shd"                 type="CT_Shd"           minOccurs="0"/>
    <xsd:element name="tblLayout"           type="CT_TblLayoutType" minOccurs="0"/>
    <xsd:element name="tblCellMar"          type="CT_TblCellMar"    minOccurs="0"/>
    <xsd:element name="tblLook"             type="CT_TblLook"       minOccurs="0"/>
    <xsd:element name="tblCaption"          type="CT_String"        minOccurs="0"/>
    <xsd:element name="tblDescription"      type="CT_String"        minOccurs="0"/>
    <xsd:element name="tblPrChange"         type="CT_TblPrChange"   minOccurs="0"/>
  </xsd:sequence>

<!-- table alignment --------------------------------- -->

<xsd:complexType name="CT_JcTable">
  <xsd:attribute name="val" type="ST_JcTable" use="required"/>
</xsd:complexType>

<xsd:simpleType name="ST_JcTable">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="center"/>
    <xsd:enumeration value="end"/>
    <xsd:enumeration value="left"/>
    <xsd:enumeration value="right"/>
    <xsd:enumeration value="start"/>
  </xsd:restriction>
</xsd:simpleType>

<!-- table width ------------------------------------- -->

<xsd:complexType name="CT_TblWidth">
  <xsd:attribute name="w"    type="ST_MeasurementOrPercent"/>
  <xsd:attribute name="type" type="ST_TblWidth"/>
</xsd:complexType>

<xsd:simpleType name="ST_MeasurementOrPercent">
  <xsd:union memberTypes="ST_DecimalNumberOrPercent s:ST_UniversalMeasure"/>
</xsd:simpleType>

<xsd:simpleType name="ST_DecimalNumberOrPercent">
  <xsd:union memberTypes="ST_UnqualifiedPercentage s:ST_Percentage"/>
</xsd:simpleType>

<xsd:simpleType name="ST_UniversalMeasure">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_UnqualifiedPercentage">
  <xsd:restriction base="xsd:integer"/>
</xsd:simpleType>

<xsd:simpleType name="ST_Percentage">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="-?[0-9]+(\.[0-9]+)?%"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_TblWidth">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="nil"/>
    <xsd:enumeration value="pct"/>
    <xsd:enumeration value="dxa"/>
    <xsd:enumeration value="auto"/>
  </xsd:restriction>
</xsd:simpleType>

<!-- table layout ------------------------------------ -->

<xsd:complexType name="CT_TblLayoutType">
  <xsd:attribute name="type" type="ST_TblLayoutType"/>
</xsd:complexType>

<xsd:simpleType name="ST_TblLayoutType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="fixed"/>
    <xsd:enumeration value="autofit"/>
  </xsd:restriction>
</xsd:simpleType>

<!-- table look -------------------------------------- -->

<xsd:complexType name="CT_TblLook">
  <xsd:attribute name="firstRow"    type="s:ST_OnOff"/>
  <xsd:attribute name="lastRow"     type="s:ST_OnOff"/>
  <xsd:attribute name="firstColumn" type="s:ST_OnOff"/>
  <xsd:attribute name="lastColumn"  type="s:ST_OnOff"/>
  <xsd:attribute name="noHBand"     type="s:ST_OnOff"/>
  <xsd:attribute name="noVBand"     type="s:ST_OnOff"/>
  <xsd:attribute name="val"         type="ST_ShortHexNumber"/>
</xsd:complexType>