一、安装

1.python-docx pypi

python-docx · PyPI

2.安装

pip install python-docx
pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple

二、官网

1.点击 Homepage

https://github.com/python-openxml/python-docx

python-docx — python-docx 0.8.11 documentation

2.快速入门

Quickstart — python-docx 0.8.11 documentation

①打开、保存

from docx import Documentdocument = Document()document.save('demo.docx')

②添加段落

document.add_paragraph('这是一段文字')

③添加图片

document.add_picture('1.jpg')

设置尺寸

from docx.shared import Inchesdocument.add_picture('image-filename.png', width=Inches(1.0))

④分页

document.add_page_break()

三、其他格式设置

from docx import Document
from docx.shared import Pt  # 设置像素、缩进等
from docx.shared import RGBColor  # 设置字体颜色
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENTdocument = Document()a = document.add_paragraph('        这是一段文字')  # 俩空格“        ”
# a.runs[0].font.bold = True #设置加粗
# a.runs[0].font.italic = True#设置斜体
# a.runs[0].font.underline = True#设置下划线
# a.runs[0].font.strike = True #设置删除线
# a.runs[0].font.shadow = True #设置文字阴影
# a.runs[0].font.size = Pt(18) #设置字号
# a.runs[0].font.color.rgb = RGBColor(255,0,255)#设置字体颜色a.runs[0].font.name = "微软雅黑"  # 设置字体 必写行1
r = a.runs[0]._element.rPr.rFonts  # 设置字体 必写行2
r.set(qn("w:eastAsia"), "微软雅黑")  # 设置字体 必写行3a.paragraph_format.line_spacing = Pt(25)  # 设置行高# 设置文字在这一行的纵向位置,通常设置段后为0,和行号,来调整行间距
# a.paragraph_format.space_before = Pt(0)  # 段前
a.paragraph_format.space_after = Pt(0)  # 段后# a.paragraph_format.left_indent = Pt(20)#左缩进
# a.paragraph_format.right_indent = Pt(20)#右缩进
a.paragraph_format.first_line_indent = Pt(20)  # 首行缩进'''
LEFT    左对齐
CENTER  居中
RIGHT   右对齐
JUSTIFY 自由调整
DISTRIBUTE  占满整个段落的宽度
JUSTIFY_MED 以中等字符压缩比调整
JUSTIFY_HI  具有高字符压缩比
JUSTIFY_LOW 以较低的字符压缩比进行调整
THAI_JUSTIFY    根据泰语格式布局对齐
'''
# a.alignment = WD_PARAGRAPH_ALIGNMENT.CENTERdocument.save('demo1.docx')

四、案例

from docx import Document
from docx.shared import Pt  # 设置像素、缩进等
from docx.shared import RGBColor  # 设置字体颜色
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENTdef setStyle_zong_title1(a):a.runs[0].font.name = "宋体"  # 设置字体 必写行1r = a.runs[0]._element.rPr.rFonts  # 设置字体 必写行2r.set(qn("w:eastAsia"), "宋体")  # 设置字体 必写行3a.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 设置居中a.runs[0].font.size = Pt(18)  # 设置字号a.paragraph_format.space_after = Pt(0)def setStyle_duan_title1(a):a.runs[0].font.name = "宋体"  # 设置字体 必写行1r = a.runs[0]._element.rPr.rFonts  # 设置字体 必写行2r.set(qn("w:eastAsia"), "宋体")  # 设置字体 必写行3a.runs[0].font.size = Pt(14)  # 设置字号a.paragraph_format.space_after = Pt(0)  # 断后def setStyle_duan_message1(a):a.runs[0].font.name = "宋体"  # 设置字体 必写行1r = a.runs[0]._element.rPr.rFonts  # 设置字体 必写行2r.set(qn("w:eastAsia"), "宋体")  # 设置字体 必写行3a.runs[0].font.size = Pt(10)  # 设置字号a.paragraph_format.space_after = Pt(0)  # 断后a.paragraph_format.first_line_indent = Pt(20)  # 首行做进if __name__ == '__main__':document = Document()a = document.add_paragraph('这是标题')setStyle_zong_title1(a)a = document.add_paragraph('一、这是标题')setStyle_duan_title1(a)a = document.add_paragraph('这是内容')setStyle_duan_message1(a)document.save('demo1.docx')

python操作word文档——python-docx相关推荐

  1. python操作word文档-python操作word

    1.Python写word文档 要操作word文档首先需要安装python-docx库: pip install python-docx 然后导入docx模块,或者从docx模块中导入Document ...

  2. python操作word文档(python-docx)

    python操作word文档(python-docx) 1. 效果图 1.1 python-docx文档标题段落(等级.加粗.斜体.居中)效果图 1.2 python-docx字体(加粗.斜体.居中. ...

  3. Python 操作Word文档插入图片和表格实例演示

    Python 操作Word文档插入图片和表格实例演示 效果图 实现过程 ① python-docx 库安装 ② word 文档插入图片演示 ③ word 文档插入表格演示 [ 文章推荐 ] Pytho ...

  4. python操作word文档中的图片_Python操作word文档插入图片和表格的实例演示

    前言P6Q免费资源网 图片是Word的一种特殊内容,这篇文章主要介绍了关于Python操作word文档,向里面插入图片和表格的相关内容,下面话不多说了,来一起看看详细的代码P6Q免费资源网 实例代码: ...

  5. 利用python操作word文档

    总目录:Python数据分析整理 本文代码参考自官方文档: python-docx 使用的库下载方式为:pip install python-docx 注意!! 不是:pip install docx ...

  6. php怎么在表格里插图片,Python操作word文档插入图片和表格的实例演示

    今天带来Python操作word文档插入图片和表格的实例演示教程详解 前言 图片是Word的一种特殊内容,这篇文章主要介绍了关于Python操作word文档,向里面插入图片和表格的相关内容,下面话不多 ...

  7. Python 操作Word文档(一)--- 基本读写

    Python 操作Word文档(一)- 基本读写 安装python-docx库 命令安装 pip install python-docx IDE中安装,我使用的是PyCharm,如下 python-d ...

  8. Java操作word文档将docx转换为pdf格式

    Java操作word文档将docx转换为pdf格式 一.整体说明 在上传 Office 课件时,格式有:doc,docx,xls,xlsx,ppt,pptx,程序需要将其 转换成 pdf 格式, 才能 ...

  9. C# DocX操作Word文档(.docx)

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  10. 一篇就够,python 操作 word 文档,使用 python-docx 落地实现,写入篇

    本篇博客为大家带来 Python 操作 docx 文档相关知识点. 主要涉及的内容为 python-docx ,一款可以操作 Word 文档(仅支持 docx)的第三方库. 文章目录 写在前面 创建一 ...

最新文章

  1. 使用深度神经网络进行自动呼叫评分(一)
  2. opengl开启垂直同步_东风悦达起亚ALL NEW K5正式定名凯酷,预售同步开启
  3. 利用Phtoshop去掉图片中的线性渐变背景
  4. 鸟哥的Linux私房菜(服务器)- 主机基本安全之一:限制 Linux 对外连线的端口
  5. 网工路由基础(5)ISIS协议
  6. 【Linux】一步一步学Linux——pstack命令(261)
  7. php cdata,PHPcdata处理(详细介绍)_PHP教程
  8. 设计: 未来Web设计的7大趋势
  9. 【原创】如何写一个框架:步骤(下)
  10. Oracle 19c 新特性:自动化索引 Automatic indexing 实践
  11. 信息技术计算机网络课堂实录,信息技术教案:计算机网络简介
  12. img标签 三种获取数据方式
  13. skinmagic对VC中程序窗口的换肤
  14. 关于FileAttributes
  15. python内置函数open_Python学习教程:Python内置函数大总结(下篇)
  16. 如何求matlab的in(2.0375),东南大学Matlab作业1.doc
  17. VO、DO、DTO、PO是什么
  18. 苹果youtube无法连接网络_当手机无信号或者无法连接网络时的正确解决步骤
  19. jQuery实用网页元素颜色修改插件
  20. isosurface matlab,matlab isosurface

热门文章

  1. Qt数据类型转换大全
  2. 如何制作美味的炸薯条图标– Photoshop教程
  3. arduino小车电机不转_一个程序玩转六个功能——赛恩司MecanBot编程小车试用测评_创意玩具...
  4. 3维空间目标跟踪的CV,CA,CT动力学模型
  5. 【iCore4 双核心板_ARM】例程一:ARM驱动三色LED
  6. [附源码]Python计算机毕业设计SSM机械零件生产管理系统(程序+LW)
  7. html中jsonp错误,解决jquery中jsonp格式跨域请求报parsererror的错误
  8. 程序的编写过程和执行过程
  9. 十九、全球经济政治社会一体化的总体发展趋势
  10. Redis 使用教程 (全)