pythondocx安装教程(Python docx库用法示例分析)
类别:脚本大全 浏览量:1705
时间:2022-04-02 10:45:15 pythondocx安装教程
Python docx库用法示例分析本文实例分析了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程序设计有所帮助。
原文链接:https://blog.csdn.net/u011932355/article/details/51769803
您可能感兴趣
- python给一个数字md5加密(Python写一个基于MD5的文件监听程序)
- python获取企业微信信息(通过shell+python实现企业微信预警)
- python 基于内容的推荐系统(不到40行代码用Python实现一个简单的推荐系统)
- python出现modify怎么办(解决Python找不到ssl模块问题 No module named _ssl的方法)
- python爬虫request方法介绍(详解Python3网络爬虫二:利用urllib.urlopen向有道翻译发送数据获得翻译结果)
- python读取文件的正确方法(强悍的Python读取大文件的解决方案)
- eval详解python(深度辨析Python的eval与exec的方法)
- python解析视频源码(基于python实现高速视频传输程序)
- python的turtle库怎么进入(python的turtle库使用详解)
- python常用列表函数和方法(Python enumerate函数功能与用法示例)
- python小程序编程代码(python实现烟花小程序)
- python爬虫并保存excel实例(Python实现爬取亚马逊数据并打印出Excel文件操作示例)
- pythonimage库(Python/Django后端使用PIL Image生成头像缩略图)
- pythonselenium隐藏浏览器窗口(Python Selenium 之关闭窗口close与quit的方法)
- python微信消息模拟请求(python实现微信机器人: 登录微信、消息接收、自动回复功能)
- pythonpandas操作拆分excel(Python使用pandas和xlsxwriter读写xlsx文件的方法示例)
- Beyond 版本《无人深空》主线任务攻略 阿特拉斯之道(版本无人深空主线任务攻略)
- 全球科技界最有钱大佬TOP 15 你知道几位(全球科技界最有钱大佬TOP)
- 2主力后腰缺阵 泰山队奇兵有望获重用,赛季0出场,迎来中超首秀(泰山队奇兵有望获重用)
- 三分71 生死战爆发 篮网旧将丁威迪今天成奇兵,助队赢球(三分71生死战爆发)
- 《红 雄安》系列广播剧第一集 水上奇兵雁翎队(雄安系列广播剧第一集)
- 小说 小伙穿越成刘备,用现代知识指挥作战,众谋士都看呆了(小伙穿越成刘备)
热门推荐
- python爬虫request方法介绍(详解Python3网络爬虫二:利用urllib.urlopen向有道翻译发送数据获得翻译结果)
- centos搭建sftp服务器(Linux云服务器搭建SFTP服务器图片服务器的操作)
- 宝塔面板进不去(宝塔面板打不开重启也不行的处理方法)
- python序列化图解(对Python3 序列解包详解)
- C#类的访问修饰符
- dedecms 数据转移(dedecms5.7 通过替换文件升级后 所有档案列表为空的解决方法)
- mysql三种查询方式(MySQL查询学习之基础查询操作)
- sql server案例(SQL Server作业报错特殊案例分析)
- css 三级层级菜单(利用CSS3实现动态的二级三级菜单效果实例源码)
- python3爬虫实例代码(python3通过selenium爬虫获取到dj商品的实例代码)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9