Python docx库用法示例分析


Posted in Python onFebruary 16, 2019

本文实例分析了Python docx库用法。分享给大家供大家参考,具体如下:

打开及保存文件:

from docx import Document
document = Document('test.docx')
document.save('test.docx')

添加文本:

document.add_paragraph('test text')

调整文本位置格式为居中:

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
document = Document('test.docx')
paragraph = document.add_paragraph('123')
paragraph.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
document.save('test.docx')

调整左缩进0.3英寸:

document = Document('test.docx')
paragraph = document.add_paragraph('this is test for left_indent with inches')
paragraph_format = paragraph.paragraph_format
paragraph_format.left_indent = Inches(0.3)
document.save('test.docx')

首行缩进:

paragraph_format.first_line_indent = Inches(0.3)

上行间距:

paragraph_format.space_before = Pt(18)

下行间距:

paragraph_format.space_after = Pt(12)

行距:

paragraph_format.line_spacing = Pt(18)

分页格式:

紧跟上段:

paragraph_format.keep_together

若本页无法完全显示,另起一页:

paragraph_format.keep_with_next

强制另起一页:

paragraph_format.page_break_before

字体格式:

p = document.add_paragraph()
run = p.add_run('test typeface')
#加粗
run.font.bold = True
#斜体
run.font.italic = True
#下划线
run.font.underline = True

WD_UNDERLINE 中有所有下划线格式

调用样例:

run.underline = WD_UNDERLINE.DOT_DASH

字体颜色:

from docx.shared import RGBColor
test = document.add_paragraph().add_run('color')
font = test.font
font.color.rgb = RGBColor(0x42, 0x24 , 0xE9)

调用预设颜色:

from docx.enum.dml import MSO_THEME_COLOR
font.color.theme_color = MSO_THEME_COLOR.ACCENT_1

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python 调用VC++的动态链接库(DLL)
Sep 06 Python
Windows下安装python2.7及科学计算套装
Mar 05 Python
在Mac OS上部署Nginx和FastCGI以及Flask框架的教程
May 02 Python
python中的lambda表达式用法详解
Jun 22 Python
Python实现将不规范的英文名字首字母大写
Nov 15 Python
python 开发的三种运行模式详细介绍
Jan 18 Python
python通过paramiko复制远程文件及文件目录到本地
Apr 30 Python
Pycharm运行加载文本出现错误的解决方法
Jun 27 Python
python3实现用turtle模块画一棵随机樱花树
Nov 21 Python
pycharm快捷键汇总
Feb 14 Python
Python各种扩展名区别点整理
Feb 27 Python
python如何使用代码运行助手
Jul 03 Python
Python中整数的缓存机制讲解
Feb 16 #Python
Python实现的爬取百度文库功能示例
Feb 16 #Python
对Python3 序列解包详解
Feb 16 #Python
对Python3 pyc 文件的使用详解
Feb 16 #Python
Python父目录、子目录的相互调用方法
Feb 16 #Python
python 获得任意路径下的文件及其根目录的方法
Feb 16 #Python
Python通过for循环理解迭代器和生成器实例详解
Feb 16 #Python
You might like
与空气斗智斗勇的经典《Overlord》,传说中的“无稽之谈”
2020/04/09 日漫
PHP JSON 数据解析代码
2010/05/26 PHP
thinkphp的URL路由规则与配置实例
2014/11/26 PHP
PHP基于接口技术实现简单的多态应用完整实例
2017/04/26 PHP
PHP htmlentities()函数用法讲解
2019/02/25 PHP
laravel 出现command not found问题的解决方案
2019/10/23 PHP
JavaScript解析URL参数示例代码
2013/08/12 Javascript
js事件绑定快捷键以ctrl+k为例
2014/09/30 Javascript
用Node.js通过sitemap.xml批量抓取美女图片
2015/05/28 Javascript
jQuery插件jRumble实现网页元素抖动
2015/06/05 Javascript
JavaScript实现强制重定向至HTTPS页面
2015/06/10 Javascript
jquery实现仿新浪微博评论滚动效果
2015/08/06 Javascript
Vue.js每天必学之组件与组件间的通信
2016/09/08 Javascript
Node.js中process模块常用的属性和方法
2016/12/13 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
浅谈vue 单文件探索
2018/09/05 Javascript
JavaScript键盘事件常见用法实例分析
2019/01/03 Javascript
[01:19]2014DOTA2国际邀请赛 采访TITAN战队ohaiyo 能赢DK很幸运
2014/07/12 DOTA
[00:30]明星选手化身超级英雄!2018DOTA2亚洲邀请赛全明星赛来临!
2018/04/06 DOTA
Python3实现购物车功能
2018/04/18 Python
python 获取键盘输入,同时有超时的功能示例
2018/11/13 Python
在pycharm中使用git版本管理以及同步github的方法
2019/01/16 Python
pandas DataFrame 删除重复的行的实现方法
2019/01/29 Python
Keras在训练期间可视化训练误差和测试误差实例
2020/06/16 Python
深入浅析css3 border-image边框图像详解
2015/11/24 HTML / CSS
详解CSS透明opacity和IE各版本透明度滤镜filter的最准确用法
2016/12/20 HTML / CSS
adidas美国官网:adidas US
2016/09/21 全球购物
如何设置Java的运行环境
2013/04/05 面试题
绩效工资实施方案
2014/03/15 职场文书
《秋游》教学反思
2014/04/24 职场文书
自我工作评价范文
2015/03/06 职场文书
pandas中DataFrame检测重复值的实现
2021/05/26 Python
Oracle 死锁的检测查询及处理
2021/09/25 Oracle
JavaScript 与 TypeScript之间的联系
2021/11/27 Javascript
一起来学习Python的元组和列表
2022/03/13 Python
openstack云计算keystone组件工作介绍
2022/04/20 Servers