python word 表格 框线_python-docx 操作word文档给表格加边框

这段代码演示了如何使用python-docx库为Word文档中的表格添加边框。通过定义`set_cell_border`函数,可以设置表格单元格的顶部、底部、左侧和右侧边框的样式、大小、颜色等属性。然后创建一个包含标题和数据的表格,并调用此函数为表头设置边框。最后保存为'demo.docx'文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码样例

# -*- coding: utf-8 -*-

"""

Created on Sat Oct 24 17:21:31 2020

pip install -i https://mirrors.aliyun.com/pypi/simple/ docx

参考文档 https://python-docx.readthedocs.io/en/latest/

@author: Administrator

"""

from docx import Document

from docx.shared import Inches

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

tc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值