如何使用python-docx库设置表格单元格的边框(How to setup cell borders with python-docx)
1.在用python-docx库插入表格后,想要更改表格样式可以通过表格样式来更改,比如:设为无线框表格
table_style = 'Normal Table'
这种方法在网上一堆,便不再赘述。但这种方法很“死板”,只能选取docx库中的指定样式,做不到根据要求隐去表格的某条边框,要实现此功能,只能自己写。
2.python-docx库官方目前没有设置单元格边框的函数方法,可以使用下列代码进行每个单元格边框的设置,用法见函数中‘Usage’字段。
from docx.table import _Cell
from docx.oxml import OxmlElement
from docx.oxml.ns import qndef set_cell_border(cell: _Cell, **kwargs):"""Set cell`s borderUsage:set_cell_border(cell,top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},bottom={"sz": 12, "color": "#00FF00", "val": "single"},start={"sz": 24, "val": "dashed", "shadow": "true"},end={"sz": 12, "val": "dashed"},)"""tc = cell._tctcPr = tc.get_or_add_tcPr()# check for tag existnace, if none found, then create onetcBorders = tcPr.first_child_found_in("w:tcBorders")if tcBorders is None:tcBorders = OxmlElement('w:tcBorders')tcPr.append(tcBorders)# list over all available tagsfor edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):edge_data = kwargs.get(edge)if edge_data:tag = 'w:{}'.format(edge)# check for tag existnace, if none found, then create oneelement = tcBorders.find(qn(tag))if element is None:element = OxmlElement(tag)tcBorders.append(element)# looks like order of attributes is importantfor key in ["sz", "val", "color", "space", "shadow"]:if key in edge_data:element.set(qn('w:{}'.format(key)), str(edge_data[key]))
3. Check http://officeopenxml.com/WPtableBorders.php for available attributes values
Elements:
| Element | Description | |
|---|---|---|
| top | Specifies the border displayed at the top of the cell. Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.75. | |
| bottom | Specifies the border displayed at the bottom of a cell. Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.3. | |
| start | Specifies the border displayed on the leading edge of the cell (left for left-to-right tables and right for right-to-left tables).
Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.34. | |
| end | Specifies the border displayed on the trailing edge of the cell (right for left-to-right tables and left for right-to-left tables).
Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.12. | |
| insideH | Specifies the border to be displayed on all interior horizontal edges of the cell. Note that this is mostly useless in the case of individual cells, as there is no concept of interior edge for individual cells. However, it is used to determine cell borders to apply to a specific group of cells as part of table conditional formatting in a table style, e.g., the inside edges on the set of cells in the first column. Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.24. | |
| insideV | Specifies the border to be displayed on all interior vertical edges of the cell. Note that this is mostly useless in the case of individual cells, as there is no concept of interior edge for individual cells. However, it is used to determine cell borders to apply to a specific group of cells as part of table conditional formatting in a table style, e.g., the inside edges on the set of cells in the first column. Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.26. | |
| tl2br | Specifies the border to be displayed on the top left side to bottom right diagonal within the cell. Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.74.
| |
| tr2bl | Specifies the border to be displayed on the top right to bottom left diagonal within the cell. Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.80. |
Attributes of child elements:
The most commonly used attributes are below. (The theme-related and frame attributes have been omitted.)
| Attribute | Description |
|---|---|
| color | Specifies the color of the border. Values are given as hex values (in RRGGBB format). No #, unlike hex values in HTML/CSS. E.g., color="FFFF00". A value of auto is also permitted and will allow the consuming word processor to determine the color. |
| shadow | Specifies whether the border should be modified to create the appearance of a shadow. For right and bottom borders, this is done by duplicating the border below and right of the normal location. For the right and top borders, this is done by moving the border down and to the right of the original location. Permitted values are true and false. |
| space | Specifies the spacing offset. Values are specified in points (1/72nd of an inch). |
| sz | Specifies the width of the border. Table borders are line borders (see the val attribute below), and so the width is specified in eighths of a point, with a minimum value of two (1/4 of a point) and a maximum value of 96 (twelve points). (Page borders can alternatively be art borders, with the width is given in points and a minimum of 1 and a maximum of 31.) |
| val | Specifies the style of the border. Table borders can be only line borders. (Page borders can also be art borders.) Possible values are:
Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.18.2. |
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

