利⽤python-docx批量处理Word⽂件——表格(⼆)样式控制表格样式可以分为两种:⼀要表格的⾃⾝的样式,⽐如边框,对齐⽅式、背景等,别⼀种是表格中⽂字的样式。本⽂的内容包括:
1. 表格样式
表格的对齐⽅式:居中、居左、居右
表格的亮度和⾼度
表格边框
2. 表格内容的样式
字体、字号
⽂字的对齐⽅式
⽂字颜⾊
⼀次性引⼊下⾯会⽤到的库:
python处理xml文件um.style import WD_STYLE_TYPE
from docx import Document
from docx.shared import Cm,Pt,RGBColor
um.table import WD_TABLE_ALIGNMENT
um.table import WD_ALIGN_VERTICAL
um.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请看
也可以⽤以下代码输出所有style的名称:
d=Document()
styles=d.styles
for s in styles:
pe==WD_STYLE_TYPE.TABLE:
print(s.name)
d.save('styl
e.docx')
1.2 ⾃定义表格样式
1.2.1 表格宽度
(1)table.autofit=True可以使表格⾃动适应窗⼝⼤⼩。
(ll(row,col).width=Cm(4)
可以设置每个单元格的宽,同列单元格宽度相同,如果定义了不同的宽度将以最⼤值准。
宽度的单位也可以是Pt或Inches
(lumns[0].width=Cm(2)
API中有这样的⽅法,但是实验发现不能⽣效,感兴趣的可以研究。
1.2.2表格⾼度
(ws[0].height=Cm(12)
宽度不能⽤的⽅法⾼度却可以⽤,单位同样可以是Pt或Inches
(ll(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)⽔平对齐⽅式
WD_ALIGN_PARAGRAPH.LEFT|WD_ALIGN_PARAGRAPH.RIGHT
(2)垂直对齐⽅式
WD_ALIGN_VERTICAL.TOP|WD_ALIGN_VERTICAL.BOTTOM
这⾥我就想吐槽⼀下,垂直⽅向的居中应该middle
(2)字体、字号和颜⾊
⽅法1:
ll(r,c).paragraphs[0].add_run(text)
run.font.name='宋体'
run.font.size=Pt(18)
b=RGBColor(233,123,12)
这种⽅法适⽤于给单元格赋值的同时修改字体样式
实验发现,字体的设置只对英⽂及数据⽣效,对汉字不⽣效
⽅法2:
table.style.font.size=Pt(18)
table.style.font.name='⿊体'
table.b=RGBColor(231,212,123)
这种⽅法可以修改整个表格的字体属性
字体的设置同样对汉字不⽣效
2.3 字体的其它属性
字体的常⽤属性docx都有定义,如果有需要再去研究。以下是table.style.font的帮助⽂档:
Help on Font in 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
| >>
| 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表格样式的控制就写这么多了,如果有其它需求⼤家可以在留⾔区提问,或着查阅相关资料。⽔平有限,还有诸多问题没有解决,欢迎交流。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论