天天看點

利用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表格樣式的控制就寫這麼多了,如果有其它需求大家可以在留言區提問,或着查閱相關資料。水準有限,還有諸多問題沒有解決,歡迎交流。

繼續閱讀