天天看点

利用python-docx批量处理Word文件——表格(二)样式控制

表格样式可以分为两种:一要表格的自身的样式,比如边框,对齐方式、背景等,别一种是表格中文字的样式。本文的内容包括:

  1. 表格样式
    • 表格的对齐方式:居中、居左、居右
    • 表格的亮度和高度
    • 表格边框
  2. 表格内容的样式
    • 字体、字号
    • 文字的对齐方式
    • 文字颜色

一次性引入下面会用到的库:

from docx.enum.style import WD_STYLE_TYPE
from docx import Document
from docx.shared import Cm,Pt,RGBColor
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.enum.text import WD_ALIGN_PARAGRAPH
           

1.表格样式

1.1 利用docx已经定义好的样式

docx定义了大量的样式库,可以直接使用,一般都能满足需求,使用方法如下:

方法1: 创建表格时设置

d=Document()
 d.add_table(2,3,style='style_name')
           

方法2: 表格创建完成后再设置

关于style_name请看python-docx表格样式列表

也可以用以下代码输出所有style的名称:

d=Document()
styles=d.styles
for s in styles:
    if s.type==WD_STYLE_TYPE.TABLE:
        print(s.name)

d.save('style.docx')           
           
1.2 自定义表格样式

1.2.1 表格宽度

(1)

table.autofit=True

可以使表格自动适应窗口大小。

(2)

table.cell(row,col).width=Cm(4)

  • 可以设置每个单元格的宽,同列单元格宽度相同,如果定义了不同的宽度将以最大值准。
  • 宽度的单位也可以是Pt或Inches

(3)

table.columns[0].width=Cm(2)

  • API中有这样的方法,但是实验发现不能生效,感兴趣的可以研究。

1.2.2表格高度

(1)

table.rows[0].height=Cm(12)

  • 宽度不能用的方法高度却可以用,单位同样可以是Pt或Inches

(2)

table.cell(row,col).height=Cm(4)

  • API有定义,但是不生效,刚好和宽度相反。

(3)这里还看到另一种方式,不过我并没有看懂,感兴趣的可以研究。

1.2.3 表格对齐方式

table.alignment=WD_TABLE_ALIGNMENT.CENTER|WD_TABLE_ALIGNMENT.LEFT|WD_TABLE_ALIGNMENT.RIGHT

1.2.4表格边框的宽度和颜色

目录还没有找到可行方法

2.表格内容样式

2.1文字的对齐方式

(1)水平对齐方式

table.cell(r,c).paragraphs[0].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.CENTER

  • WD_ALIGN_PARAGRAPH.LEFT|WD_ALIGN_PARAGRAPH.RIGHT

(2)垂直对齐方式

table.cell(r,c).vertical_alignment = WD_ALIGN_VERTICAL.CENTER

  • WD_ALIGN_VERTICAL.TOP|WD_ALIGN_VERTICAL.BOTTOM

  • 这里我就想吐槽一下,垂直方向的居中应该middle

(2)字体、字号 和颜色

方法1:

run=tb.cell(r,c).paragraphs[0].add_run(text)
run.font.name='宋体'
run.font.size=Pt(18)
run.font.color.rgb=RGBColor(233,123,12)
           
  • 这种方法适用于给单元格赋值的同时修改字体样式
  • 实验发现,字体的设置只对英文及数据生效,对汉字不生效

方法2:

table.style.font.size=Pt(18)
table.style.font.name='黑体'
table.style.font.color.rgb=RGBColor(231,212,123)
           
  • 这种方法可以修改整个表格的字体属性
  • 字体的设置同样对汉字不生效
2.3 字体的其它属性

字体的常用属性docx都有定义,如果有需要再去研究。以下是table.style.font的帮助文档:

Help on Font in module docx.text.font object:

class Font(docx.shared.ElementProxy)

| Proxy object wrapping the parent of a

<w:rPr>

element and providing

| access to character properties such as font name, font size, bold, and

| subscript.

|

| Method resolution order:

| Font

| docx.shared.ElementProxy

| builtins.object

|

| Data descriptors defined here:

|

| all_caps

| Read/write. Causes text in this font to appear in capital letters.

|

| bold

| Read/write. Causes text in this font to appear in bold.

|

| color

| A |ColorFormat| object providing a way to get and set the text color

| for this font.

|

| complex_script

| Read/write tri-state value. When |True|, causes the characters in the

| run to be treated as complex script regardless of their Unicode

| values.

|

| cs_bold

| Read/write tri-state value. When |True|, causes the complex script

| characters in the run to be displayed in bold typeface.

|

| cs_italic

| Read/write tri-state value. When |True|, causes the complex script

| characters in the run to be displayed in italic typeface.

|

| double_strike

| Read/write tri-state value. When |True|, causes the text in the run

| to appear with double strikethrough.

|

| emboss

| Read/write tri-state value. When |True|, causes the text in the run

| to appear as if raised off the page in relief.

|

| hidden

| Read/write tri-state value. When |True|, causes the text in the run

| to be hidden from display, unless applications settings force hidden

| text to be shown.

|

| highlight_color

| A member of :ref:

WdColorIndex

indicating the color of highlighting

| applied, or

None

if no highlighting is applied.

|

| imprint

| Read/write tri-state value. When |True|, causes the text in the run

| to appear as if pressed into the page.

|

| italic

| Read/write tri-state value. When |True|, causes the text of the run

| to appear in italics. |None| indicates the effective value is

| inherited from the style hierarchy.

|

| math

| Read/write tri-state value. When |True|, specifies this run contains

| WML that should be handled as though it was Office Open XML Math.

|

| name

| Get or set the typeface name for this |Font| instance, causing the

| text it controls to appear in the named font, if a matching font is

| found. |None| indicates the typeface is inherited from the style

| hierarchy.

|

| no_proof

| Read/write tri-state value. When |True|, specifies that the contents

| of this run should not report any errors when the document is scanned

| for spelling and grammar.

|

| outline

| Read/write tri-state value. When |True| causes the characters in the

| run to appear as if they have an outline, by drawing a one pixel wide

| border around the inside and outside borders of each character glyph.

|

| rtl

| Read/write tri-state value. When |True| causes the text in the run

| to have right-to-left characteristics.

|

| shadow

| Read/write tri-state value. When |True| causes the text in the run

| to appear as if each character has a shadow.

|

| size

| Read/write |Length| value or |None|, indicating the font height in

| English Metric Units (EMU). |None| indicates the font size should be

| inherited from the style hierarchy. |Length| is a subclass of |int|

| having properties for convenient conversion into points or other

| length units. The :class:

docx.shared.Pt

class allows convenient

| specification of point values::

|

| >> font.size = Pt(24)

| >> font.size

| 304800

| >> font.size.pt

| 24.0

|

| small_caps

| Read/write tri-state value. When |True| causes the lowercase

| characters in the run to appear as capital letters two points smaller

| than the font size specified for the run.

|

| snap_to_grid

| Read/write tri-state value. When |True| causes the run to use the

| document grid characters per line settings defined in the docGrid

| element when laying out the characters in this run.

|

| spec_vanish

| Read/write tri-state value. When |True|, specifies that the given run

| shall always behave as if it is hidden, even when hidden text is

| being displayed in the current document. The property has a very

| narrow, specialized use related to the table of contents. Consult the

| spec (§17.3.2.36) for more details.

|

| strike

| Read/write tri-state value. When |True| causes the text in the run

| to appear with a single horizontal line through the center of the

| line.

|

| subscript

| Boolean indicating whether the characters in this |Font| appear as

| subscript. |None| indicates the subscript/subscript value is

| inherited from the style hierarchy.

|

| superscript

| Boolean indicating whether the characters in this |Font| appear as

| superscript. |None| indicates the subscript/superscript value is

| inherited from the style hierarchy.

|

| underline

| The underline style for this |Font|, one of |None|, |True|, |False|,

| or a value from :ref:

WdUnderline

. |None| indicates the font

| inherits its underline value from the style hierarchy. |False|

| indicates no underline. |True| indicates single underline. The values

| from :ref:

WdUnderline

are used to specify other outline styles such

| as double, wavy, and dotted.

|

| web_hidden

| Read/write tri-state value. When |True|, specifies that the contents

| of this run shall be hidden when the document is displayed in web

| page view.

后记:关于python-docx表格样式的控制就写这么多了,如果有其它需求大家可以在留言区提问,或着查阅相关资料。水平有限,还有诸多问题没有解决,欢迎交流。

继续阅读