应用格式设置

表格的每个元素都可以应用不同的格式。例如,表格格式设置将应用于整个表格,行格式设置将仅应用于特定行,单元格格式设置将仅应用于某些单元格。

Aspose.Words 提供丰富的 API 来检索表格格式并将其应用到表格。您可以使用 TableRowFormatCellFormat 节点来设置格式。

在本文中,我们将讨论如何将格式应用于不同的表节点以及 Aspose.Words 支持哪些表格式设置。

将格式应用于不同的节点

在本节中,我们将研究如何将格式应用于各种表节点。

表级格式化

要将格式应用于表,您可以使用 TablePreferredWidthTableCollection 类的相应 Table 节点上可用的属性。

下图显示了 Microsoft Word 中的 Table 格式化功能及其在 Aspose.Words 中的相应属性。

formattin-features-table-level-aspose-words-python

formatting-table-options-aspose-words-python

以下代码示例演示如何将轮廓边框应用于表格:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Align the table to the center of the page.
table.alignment = aw.tables.TableAlignment.CENTER
# Clear any existing borders from the table.
table.clear_borders()
# Set a green border around the table but not inside.
table.set_border(aw.BorderType.LEFT, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True)
table.set_border(aw.BorderType.RIGHT, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True)
table.set_border(aw.BorderType.TOP, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True)
table.set_border(aw.BorderType.BOTTOM, aw.LineStyle.SINGLE, 1.5, drawing.Color.green, True)
# Fill the cells with a light green solid color.
table.set_shading(aw.TextureIndex.TEXTURE_SOLID, drawing.Color.light_green, drawing.Color.empty())
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.apply_outline_border.docx")

以下代码示例显示如何构建启用所有边框的表格(网格):

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Clear any existing borders from the table.
table.clear_borders()
# Set a green border around and inside the table.
table.set_borders(aw.LineStyle.SINGLE, 1.5, drawing.Color.green)
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.build_table_with_borders.docx")

行级格式化

行级 格式可以使用 RowRowFormatRowCollection 类进行控制。

下图显示了 Microsoft Word 中的 Row 格式功能及其在 Aspose.Words 中的相应属性。

formatting-row-level-aspose-words-python

以下代码示例显示如何修改表行格式:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Retrieve the first row in the table.
first_row = table.first_row
first_row.row_format.borders.line_style = aw.LineStyle.NONE
first_row.row_format.height_rule = aw.HeightRule.AUTO
first_row.row_format.allow_break_across_pages = True

单元格级别格式化

单元格级格式由 CellCellFormatCellCollection 类控制。

下图显示了 Microsoft Word 中的 Cell 格式功能及其在 Aspose.Words 中的相应属性。

formatting-cell-level-aspose-words-python

auto-formatting-cell-level-aspose-words-python

以下代码示例显示如何修改表格单元格的格式:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
first_cell = table.first_row.first_cell
first_cell.cell_format.width = 30
first_cell.cell_format.orientation = aw.TextOrientation.DOWNWARD
first_cell.cell_format.shading.foreground_pattern_color = drawing.Color.light_green

以下代码示例演示如何设置添加到单元格内容的左侧/顶部/右侧/底部的空间量(以磅为单位):

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.start_table()
builder.insert_cell()
# Sets the amount of space (in points) to add to the left/top/right/bottom of the cell's contents.
builder.cell_format.set_paddings(30, 50, 30, 50)
builder.writeln("I'm a wonderful formatted cell.")
builder.end_row()
builder.end_table()
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.cell_padding.docx")
view raw cell-padding.py hosted with ❤ by GitHub

指定行高

设置行高的最简单方法是使用 DocumentBuilder。使用适当的 RowFormat 属性,您可以设置默认高度设置或为表中的每一行应用不同的高度。

在 Aspose.Words 中,表格行高由以下因素控制:

同时,可以为每行设置不同的高度 - 这使您可以广泛控制表格设置。

以下代码示例演示如何创建包含单个单元格的表格并应用行格式:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
table = builder.start_table()
builder.insert_cell()
row_format = builder.row_format
row_format.height = 100
row_format.height_rule = aw.HeightRule.EXACTLY
# These formatting properties are set on the table and are applied to all rows in the table.
table.left_padding = 30
table.right_padding = 30
table.top_padding = 30
table.bottom_padding = 30
builder.writeln("I'm a wonderful formatted row.")
builder.end_row()
builder.end_table()
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.apply_row_formatting.docx")

指定表格和单元格宽度

Microsoft Word 文档中的表格提供了几种不同的方法来调整表格和单个单元格的大小。这些属性允许对表的外观和行为进行大量控制,以便 Aspose.Words 支持表的行为,如 Microsoft Word 中一样。

重要的是要知道表格元素提供了几个不同的属性,这些属性可能会影响整个表格以及单个单元格的宽度的计算方式:

  • 桌子上的首选宽度
  • 单个单元格的首选宽度
  • 允许在桌子上自动调整

本文详细介绍了各种表格宽度计算属性的工作原理以及如何完全控制表格宽度计算。这是 在表格布局未按预期显示的情况下了解这一点尤其有用。

如何使用首选宽度

表格或单个单元格的所需宽度是通过首选宽度属性定义的,该属性是元素努力适应的大小。也就是说,可以为整个表格或单个单元格指定首选宽度。在某些情况下,可能无法完全适合该宽度,但大多数情况下实际宽度会接近该值。

使用 PreferredWidth 类的方法设置适当的首选宽度类型和值:

  • Auto 方法指定自动或"无首选宽度"
  • FromPercent 方法指定百分比宽度
  • FromPoints 方法指定宽度(以磅为单位)

下图显示了 Microsoft Word 中的首选宽度设置功能及其在 Aspose.Words 中的相应属性。

formatting-table-properties-aspose-words-python

下图显示了如何将这些选项应用于文档中的真实表格的示例。

tables-applying-options-python

指定首选表格或单元格宽度

在 Aspose.Words 中,表格和单元格宽度是使用 Table.PreferredWidth 属性和 CellFormat.PreferredWidth 属性设置的,并在 PreferredWidthType 枚举中提供可用选项:

  • Auto,相当于没有设置首选宽度
  • Percent,使元素相对于窗口或容器中的可用空间大小进行拟合,并在可用宽度发生变化时重新计算该值
  • Points,对应于以点为单位的指定宽度的元素

使用 Table.PreferredWidth 属性将调整其相对于其容器的首选宽度:页面、文本列或外部表格单元格(如果它是嵌套表格)。

以下代码示例演示如何将表格设置为自动适应页面宽度的 50%:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Autofit the first table to the page width.
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_WINDOW)
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_window.docx")

在给定单元格上使用 CellFormat.PreferredWidth 属性将调整其首选宽度。

以下代码示例显示如何设置不同的首选宽度设置:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Insert a table row made up of three cells which have different preferred widths.
builder.start_table()
# Insert an absolute sized cell.
builder.insert_cell()
builder.cell_format.preferred_width = aw.tables.PreferredWidth.from_points(40)
builder.cell_format.shading.background_pattern_color = drawing.Color.light_yellow
builder.writeln("Cell at 40 points width")
# Insert a relative (percent) sized cell.
builder.insert_cell()
builder.cell_format.preferred_width = aw.tables.PreferredWidth.from_percent(20)
builder.cell_format.shading.background_pattern_color = drawing.Color.light_blue
builder.writeln("Cell at 20% width")
# Insert a auto sized cell.
builder.insert_cell()
builder.cell_format.preferred_width = aw.tables.PreferredWidth.AUTO
builder.cell_format.shading.background_pattern_color = drawing.Color.light_green
builder.writeln(
"Cell automatically sized. The size of this cell is calculated from the table preferred width.")
builder.writeln("In this case the cell will fill up the rest of the available space.")
doc.save(ARTIFACTS_DIR + "WorkingWithTables.preferred_width_settings.docx")

查找首选宽度类型和值

您可以使用 TypeValue 属性来查找所需表格或单元格的首选宽度详细信息。

以下代码示例显示如何检索表格单元格的首选宽度类型:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
table.allow_auto_fit = True
first_cell = table.first_row.first_cell
type_ = first_cell.cell_format.preferred_width.type
value = first_cell.cell_format.preferred_width.value

如何设置自动调整

AllowAutoFit 属性允许表中的单元格根据选定的标准增大和缩小。例如,您可以使用 自动适应窗口 选项使表格适合页面的宽度,并使用 自动适应内容 选项允许每个单元格根据其内容增大或缩小。

默认情况下,Aspose.Words 使用 自动适应窗口 插入新表。表格的大小将根据可用的页面宽度进行调整。要调整表的大小,您可以调用 AutoFit 方法。此方法接受 AutoFitBehavior 枚举,该枚举指定对表应用什么类型的自动调整。

重要的是要知道 autofit 方法实际上是一种同时将不同属性应用于表的快捷方式。这些属性实际上为表提供了观察到的行为。我们将讨论每个自动调整选项的这些属性。

以下代码示例显示如何设置表格以根据其内容缩小或增大每个单元格:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
table.allow_auto_fit = True

自动调整表格到窗口

当自动适应窗口应用于表时,实际上在幕后执行以下操作:

  1. 启用 Table.AllowAutoFit 属性以自动调整列大小以适应可用内容,使用 Table.PreferredWidth 值 100%
  2. CellFormat.PreferredWidth从所有表格单元中删除
  3. 针对当前表格内容重新计算列宽 – 最终结果是表格占据整个可用宽度 4.表格中的列宽会随着用户编辑文本而自动改变

以下代码示例演示如何使表格自动适应页面宽度:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Autofit the first table to the page width.
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_WINDOW)
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_window.docx")

自动调整表格以适应内容

当表格自动调整内容时,实际上在幕后执行以下步骤:

1.启用Table.AllowAutoFit属性,根据其内容自动调整每个单元格的大小

2.从Table.PreferredWidth中删除首选表格宽度,为每个表格单元格删除CellFormat.PreferredWidth

  1. 针对当前表格内容重新计算列宽 – 最终结果是一个表格,其中列宽和整个表格的宽度会在用户编辑文本时自动调整大小以最适合内容

以下代码示例演示如何自动调整表格以适应其内容:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_CONTENTS)
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_contents.docx")

禁用表格中的自动调整并使用固定列宽

如果表禁用了自动调整并使用固定列宽,则执行以下步骤:

  1. Table.AllowAutoFit 属性被禁用,因此列不会根据其内容增大或缩小 2.从Table.PreferredWidth中删除整个表格的首选宽度,从所有表格单元格中删除CellFormat.PreferredWidth
  2. 最终结果是一个表格,其列宽由 CellFormat.Width 属性确定,并且当用户输入文本或调整页面大小时,其列不会自动调整大小

以下代码示例显示如何禁用自动调整并为指定表格启用固定宽度:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Disable autofitting on this table.
table.auto_fit(aw.tables.AutoFitBehavior.FIXED_COLUMN_WIDTHS)
doc.save(ARTIFACTS_DIR + "WorkingWithTables.auto_fit_table_to_fixed_column_widths.docx")

计算单元格宽度时的优先顺序

Aspose.Words 允许用户通过多个对象(包括 CellFormat)定义表格或单元格的宽度 - 它的 Width 属性大部分是以前版本遗留下来的,但是,它对于简化单元格宽度的设置仍然很有用。

重要的是要知道 CellFormat.Width 属性的工作方式有所不同,具体取决于表中已存在的其他宽度属性。

Aspose.Words 使用以下顺序来计算单元格宽度:

命令 财产 描述
1 AllowAutoFit确定 如果启用 AutoFit
- 表格可能会增长超过首选宽度以容纳内容 - 它通常不会缩小到首选宽度以下
- 对 CellFormat.Width 值的任何更改都会被忽略,单元格将适合其内容
2 值为 PointsPercentPreferredWidthType CellFormat.Width 被忽略
3 PreferredWidthType 值为 Auto CellFormat.Width 中的值被复制并成为单元格的首选宽度(以磅为单位)

允许单元格之间有间距

您可以获取或设置表格单元格之间的任何附加空间,类似于 Microsoft Word 中的"Cell 间距"选项。这可以使用 AllowCellSpacing 属性来完成。

下图显示了如何将这些选项应用于文档中的真实表格的示例。

格式化-单元格之间的间距-aspose-words-python

以下代码示例展示了如何设置单元格之间的间距:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
table.allow_cell_spacing = True
table.cell_spacing = 2
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.allow_cell_spacing.docx")

应用边框和底纹

边框和底纹可以使用 Table.SetBorderTable.SetBordersTable.SetShading 应用于整个表格,也可以使用 CellFormat.BordersCellFormat.Shading 仅应用于特定单元格。此外,可以使用 RowFormat.Borders 设置行边框,但不能以这种方式应用阴影。

下图显示了 Microsoft Word 中的边框和阴影设置以及 Aspose.Words 中相应的属性。

formatting-border-line-aspose-words-python

formatting-cell-color-aspose-words-python

以下代码示例演示如何设置具有不同边框和底纹的表格和单元格的格式:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
table = builder.start_table()
builder.insert_cell()
# Set the borders for the entire table.
table.set_borders(aw.LineStyle.SINGLE, 2.0, drawing.Color.black)
# Set the cell shading for this cell.
builder.cell_format.shading.background_pattern_color = drawing.Color.red
builder.writeln("Cell #1")
builder.insert_cell()
# Specify a different cell shading for the second cell.
builder.cell_format.shading.background_pattern_color = drawing.Color.green
builder.writeln("Cell #2")
builder.end_row()
# Clear the cell formatting from previous operations.
builder.cell_format.clear_formatting()
builder.insert_cell()
# Create larger borders for the first cell of this row. This will be different
# compared to the borders set for the table.
builder.cell_format.borders.left.line_width = 4.0
builder.cell_format.borders.right.line_width = 4.0
builder.cell_format.borders.top.line_width = 4.0
builder.cell_format.borders.bottom.line_width = 4.0
builder.writeln("Cell #3")
builder.insert_cell()
builder.cell_format.clear_formatting()
builder.writeln("Cell #4")
doc.save(ARTIFACTS_DIR + "WorkingWithTableStylesAndFormatting.format_table_and_cell_with_different_borders.docx")