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. 弹出popwindow 背景变暗
  2. VMware Virtual SAN存储设计规划
  3. 使用LINQ解除SQL注入安全问题
  4. Could not find a version that satisfies the requirement requests (from version : )
  5. 任务型对话系统预训练最新研究进展
  6. 机器学习 可视化_机器学习-可视化
  7. Windows XP中快速识别真假SVCHOST.EXE
  8. Convert to Ones(CF-998C)
  9. Android 为View实现双击效果
  10. canvas背景效果
  11. 美化博客园 添加网易云音乐及生成文章目录
  12. Ubuntu系统盘制作
  13. 公开课丨重中之重!Web安全漏洞与防御
  14. 买房的疯了-开发商笑不起来了(本文仅供新浪)
  15. 成都工业学院(成都无机校/电子机械高等专科学校)百年校庆之行记
  16. 海贼王python代码_这顶海贼王的帽子,我Python给你带上了 | 【人脸识别应用】
  17. Linux命令老是记不住?一篇文章帮你解决。Linux常用命令汇总
  18. python基础知识之整除、取余、幂运算
  19. 概率论基础(4)五种重要的分布(二项、泊松、均匀、指数、正态分布)
  20. 软银集团和共享办公空间公司WeWork在日本成立合资公司

热门文章

  1. C# 7-5 公民类Citizen (20 分)
  2. Android从图库中选择图片并作为Activity的背景(主题)
  3. c语言:求某年某月的天数(2种方法)
  4. Fastjson介绍
  5. 从0到1 :中企动力进入服务的“管家”时代
  6. C语言编程>第二十二周 ① 下列给定程序中,函数fun的功能是:找出一个大于给定整数n且紧随n的素数,并作为函数值返回。
  7. 一箭穿心的c语言编程,《用C语言函数实现心形图案和一箭穿心图案的显示》.pdf...
  8. 4.Vue项目中下载本地pd、word、excel文件
  9. 新的开始,新的希望!
  10. Excel表格中“平方、立方”单位如何正确输入