# -*- 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
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])) document = Document()
#document.add_picture('monty-truth.png', width=Inches(1.25)) records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
) table = document.add_table(rows=1, cols=1)
hdr_cells = table.rows[0].cells
hdr_cells[0].text ="xxx" set_cell_border(
hdr_cells[0],
top={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
bottom={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
left={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
right={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
insideH={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"},
end={"sz": 0.5, "val": "double", "color": "#000000", "space": "0"}
) # hdr_cells[1].text = 'Id'
# hdr_cells[2].text = 'Desc'
# for qty, id, desc in records:
# row_cells = table.add_row().cells
# row_cells[0].text = str(qty)
# row_cells[1].text = id
# row_cells[2].text = desc print('finish !!!')

参考:http://officeopenxml.com/WPtableBorders.php

python-docx 设置表格边框的更多相关文章

  1. css中的border-collapse属性如何设置表格边框线?(代码示例)

    css中的border-collapse属性如何设置表格边框线?本篇文章就给大家介绍css中的border-collapse属性是什么? border-collapse属性设置表格边框线的方法.有一定 ...

  2. CSS:表格样式(设置表格边框/文字/背景的样式)

    使用CSS能够制作出十分精美的表格. 代码整理自w3school:http://www.w3school.com.cn 效果图: 代码: <!DOCTYPE html PUBLIC " ...

  3. PHPExcel 设置表格边框

    //设置单元格边框 $style_array = array( 'borders' => array( 'allborders' => array( 'style' => \PHPE ...

  4. 设置表格边框css样式

    table{ width:70%; text-align:center; border-left:#C8B9AE solid 1px; border-top:#C8B9AE solid 1px; bo ...

  5. [转]CSS如何设置html table表格边框样式

    原文地址:http://www.divcss5.com/wenji/w503.shtml 对table设置css样式边框,分为几种情况: 1.只对table设置边框 2.对td设置边框 3.对tabl ...

  6. HTML表格边框的设置小技巧

    对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 对于很多初学HTML的人来说,表格<table>是最常用的标签了 ...

  7. (转)HTML表格边框的设置小技巧

    对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 对于很多初学HTML的人来说,表格<table>是最常用的标签了 ...

  8. HTML表格边框的设置小技巧-表格

    对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 一般我们用表格的时候总会给它个border属性,比如:<table b ...

  9. html 表格边框的设置

    对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 一般我们用表格的时候总会给它个border属性,比如:<table b ...

  10. python 处理 Excel 表格

    see: http://www.cnblogs.com/sunada2005/p/3193300.html 一.可使用的第三方库 python中处理excel表格,常用的库有xlrd(读excel)表 ...

随机推荐

  1. 全网最简单DeepSeek-R1本地部署教程

    1.安装ollama 打开ollama网址:https://ollama.com/ 选择你电脑的系统进行下载 我的电脑是windows的就点击windows然后点击下载即可 下载完毕后双击打开下载的. ...

  2. Windows下安装和配置NodeJS

    1.下载 中文地址:https://nodejs.cn/download/ 官网地址:https://nodejs.org/en/download/ 如这里我们下载当前最新版:https://node ...

  3. 数据同步-同步mysql到iceberg后如何确定数据一致性

    一.数据打快照做数据比较 1.mysql创建快照 优点:可以选择时间做快照,然后对比 缺点:需要额外的存储空间和处理时间,不好自动化,大表做快照成本高 2.实现方式 create database 快 ...

  4. MacSecureCRT配置

    一.调整连接服务器超时时间 Options->Global Options->General->Default Session-> 右侧Edit Default Setting ...

  5. 旅行商问题(TSP)概述

    旅行商问题(TSP)概述 1. TSP问题的复杂性 定义:旅行商问题(Traveling Salesman Problem, TSP)是给定一系列城市及其之间的距离,要求找到一条最短路径,使得旅行商从 ...

  6. 什么是OpenStack?

    OpenStack是一个云平台管理的项目,它不是一个软件,它是由几个主要的组件组合起来,为公有云.私有云和混合云的建设与管理提供软件的开源项目.现在已经有来自100多个国家的数万名个人和200多家企业 ...

  7. .NET Core常用集合的几个坑

    C#中的常见集合 注意,箭头线不代表继承关系,只代表功能上的加强,如有错误,欢迎指出. 泛型集合时间复杂度 集合类型 添加 删除 查找 访问(索引/键) 遍历 备注 List<T> O(1 ...

  8. [BZOJ3160] 万径人踪灭 题解

    首先正难则反,想到答案即为满足第一条要求的回文子序列数量,减去回文子串数量.回文子串数量 \(hash+\) 二分即可,考虑前半部分. 假如我们将一个回文子序列一层层剥开,就会发现它其实是由多个相同的 ...

  9. DeepSeek本地部署

    一.ollama ollama是一个管理和运行所有大模型.开源大模型的平台.在官网的Models中可以看到deepseek-r1的AI模型 1.在官网中下载对应系统的ollama,下载时需要开墙,或者 ...

  10. SMOTE算法解决样本不平衡

    首先,看下Smote算法之前,我们先看下当正负样本不均衡的时候,我们通常用的方法: 抽样 常规的包含过抽样.欠抽样.组合抽样 过抽样:将样本较少的一类sample补齐 欠抽样:将样本较多的一类samp ...