曾經(jīng)在網(wǎng)上苦苦尋找過(guò)python docx對(duì)word表格邊框的設(shè)置,一直沒有,現(xiàn)在終于有了,包括邊框顏色、線型、寬度粗細(xì)的設(shè)置,直接用這個(gè)函數(shù)
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
def set_cell_border(cell, **kwargs):
'''
Set cell`s border
Usage:
set_cell_border(
cell,
top={'sz': 12, 'val': 'single', 'color': '#FF0000', 'space': '0'},
bottom={'sz': 12, 'color': '#00FF00', 'val': 'single'},
left={'sz': 24, 'val': 'dashed', 'shadow': 'true'},
right={'sz': 12, 'val': 'dashed'},
)
'''
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# check for tag existnace, if none found, then create one
tcBorders = tcPr.first_child_found_in('w:tcBorders')
if tcBorders is None:
tcBorders = OxmlElement('w:tcBorders')
tcPr.append(tcBorders)
# list over all available tags
for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):
edge_data = kwargs.get(edge)
if edge_data:
tag = 'w:{}'.format(edge)
# check for tag existnace, if none found, then create one
element = tcBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tcBorders.append(element)
# looks like order of attributes is important
for key in ['sz', 'val', 'color', 'space', 'shadow']:
if key in edge_data:
element.set(qn('w:{}'.format(key)), str(edge_data[key]))
|
|
來(lái)自: 新用戶61024634 > 《py office》