利用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: 表格創建完成后再設置

table.style='style_name'

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM