Python-docx 安装使用说明

Python-docx模块下载

进入 https://pypi.org/project/python-docx/ ,使用pip安装或是下载压缩包进行安装
1、pip install python-docx

2、下载压缩包

Python-docx模块使用

导入Document类
from docx import Document

新建一个空白Word文档
document = Document()
https://python-docx.readthedocs.io/en/latest/api/document.html#id1

设置文档页边距
#导入inches
from docx.shared import Inches

https://python-docx.readthedocs.io/en/latest/api/section.html#sections-objects
#设置上下左右页边距
sections = document.sections
section = sections[0]
section.top_margin = Inches(0.7)
section.bottom_margin = Inches(0.7)
section.left_margin = Inches(0.7)
section.right_margin = Inches(0.7)

红色箭头为页边距Inches(0.7),可根据实际设置值

设置标题
使用Document提供的add_heading方法
h = document.add_heading(“标题标题标题”)

添加段落
使用Document提供的add_paragraph方法
p = document.add_paragraph(“段落段落段落”)

段落属性设置
#导入WD_ALIGN_PARAGRAPH,Pt
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH

更多属性参考 https://python-docx.readthedocs.io/en/latest/api/text.html#paragraphformat-objects

#段落格式(段落的对齐设置为居中。此段落与后续段落之间出现的间距的值设为0)
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)

#段落字体大小设置
p.style.font.size = Pt(18)

添加表格
https://python-docx.readthedocs.io/en/latest/api/table.html#id1
使用Document提供的add_table方法
#添加一个4行4列表格
table = document.add_table(rows=4,cols=4,style=“Table Grid”)

#合并单元格(cell(x,y)中 x代表行,y代表列,都从0开始,比如第一行第一列即为cell(0,0))
table.cell(0,0).merge(table.cell(0,1))
table.cell(1,2).merge(table.cell(1,3))
table.cell(2,0).merge(table.cell(3,0))
table.cell(2,2).merge(table.cell(3,2))

设置单元格底纹(好像还没有开放相应接口)
参考 https://groups.google.com/forum/#!topic/python-docx/-c3OrRHA3q
#导入
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
#设置单元格cell(0,0)底纹
shading = parse_xml(r’<w:shd {} w:fill=“E5DFEC”/>’.format(nsdecls(‘w’)))
table.cell(0,0)._tc.get_or_add_tcPr().append(shading)
设置多个单元格不能用同一个shading,更换一下变量名,否则只有最后一个应用shading的生效

保存
使用Document提供的save方法
#可填实际存放路径,只填文件名,则保存在调用程序当前文件夹中
document.save(‘test.docx’)


Python-docx相关推荐

  1. python docx 设置字体_python docx 中文字体设置的操作方法

    这篇文章主要介绍了关于python docx 中文字体设置的操作方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 最近用到了docx生成word文档,docx本身用起来很方便,自带的 ...

  2. python docx库使用样例_Python docx库用法示例分析

    本文实例分析了Python docx库用法.分享给大家供大家参考,具体如下: 打开及保存文件: from docx import Document document = Document('test. ...

  3. python字体加粗代码_如何在python docx中加粗行单元格的文本?

    在python docx中,可以通过使用 Rich Text 造型.您应该为模板中需要样式化的特定字符/字符串在字符/字符串的位置提供一个上下文变量.此变量映射到 RichText 具有样式定义(在代 ...

  4. python docx模块pip安装_python安装docx模块

    python docx 怎么安装 如何安装python docx 直接 pip install docxor python -m pip install docxor sudo python -m p ...

  5. python docx tables_pythondocx保留格式替换表格内容

    如何用python-docx替换一个docx文本中的指定字符在你准备骗我之前,请做好我可能永远不会原谅你的准备. 假设我有一个doc文档,里面有一篇小短文(注意是短文不是表格),我想把from doc ...

  6. python docx模块如何删除段落_在pythondocx中更改段落格式

    我试图使用Python的Python docx模块更改多个段落的格式.在from docx import Document from docx.shared import Pt from docx.s ...

  7. doc python 字体颜色,Python docx修改文字大小字体类型 Python-docx 实现整体修改或者部分修改文字的大小和字体类型...

    想了解Python-docx 实现整体修改或者部分修改文字的大小和字体类型的相关内容吗,Leeoo_lyq在本文为您仔细讲解Python docx修改文字大小字体类型的相关知识和一些Code实例,欢迎 ...

  8. Python docx添加表格后,对表格首行设置背景色

    用python docx设置word中表格的背景色 问题来源: https://stackoverflow.com/questions/26752856/python-docx-set-table-c ...

  9. python docx 批量修改docx文件内容和选择位置添加内容

    #-*- encoding:utf_8 -*- from docx import Document from docx.shared import Inches from log import log ...

  10. python docx设置标题中文字体rPr.rFonts.set(qn(‘w:eastAsia‘),u‘黑体‘),报错‘NoneType‘ object has no attribute ‘set‘

    一.我的代码: 因为要编一个报告自动化生成的代码,所以用了python docx,在设置标题自己写了一段代码,要求将标题设置成:一级标题 黑体 小四 from docx import Document ...

最新文章

  1. 性能测试学习过程中遇到的问题与解答1
  2. 【技术贴】火狐的悬停激活标签扩展插件下载。Tab Focus
  3. TensorFlow练习26: AI操盘手
  4. spring+struts+hibernate分页 完整版,项目使用中
  5. VS2019配置库文件
  6. 吴恩达机器学习Ex3作业
  7. 年轻人的第一台车“没了”,小米回应:新媒体同学抖错了机灵
  8. SQL注入详解,看这篇就够了
  9. MFC 进行界面设计与编程
  10. android addr2line使用
  11. MySQL的三层架构(连接认证、解析优化和存储引擎)
  12. outlook邮箱邮件大小限制_outlook 附件大小限制
  13. Lpc1768 常见错误及解决方法
  14. ros开发增加clion常用模板及初始化配置(二)
  15. java——OOA,OOD,OOP
  16. 790-C语言的数组元素下标为何从0开始?
  17. 【微信】PC端多开设置
  18. C4D R22插件SolidAngle Cinema 4D To Arnold for mac(C4DtoA阿诺德渲染工具)
  19. Python练习实例——判断奇偶数
  20. STM8L151 使用硬件SPI驱动W25Q16 Flash

热门文章

  1. CSS伪元素插入文字
  2. 工业机器人电柜布线_工业机器人控制柜使用方法
  3. AccessClient_WinV1.8.00003,华为云客户端 Windows 版
  4. 102届秋季广交会参展准入管理规定 2007年广交会参展准入资质标准
  5. 数字音频光端机的工作原理及其应用介绍
  6. 计算几何--凸包之graham scan算法
  7. tkinter如何变成悬浮窗
  8. 西交大计算机学硕是公费吗,西工大硕士生待遇太差,和交大没法比,建议学弟学妹不要上,切记,欢迎顶贴,标题要长...
  9. linux输入法管理员权限,Linux 输入法设置
  10. RFSoC应用笔记 - RF数据转换器 -02- IP配置指南