
一、安装Python-docx
Python-docx是专门针对于word文档的一个模块,只能读取docx不能读取doc文件。说白了,python就相当于windows操作系统。
1.1、安装Python-docx
1.1.1、使用虚拟环境安装python-docx
1 | pip install python-docx # 安装命令
|
安装结束后,在此虚拟环境中运行Jupyter notebook
1.1.2、切换工作目录(使用 %cd命令)
1 2 3 | %cd F:python_test1Python_office # 进入目录
%pwd # 查看当前的工作目录
>>> 'F:\python_test1\Python_office' # 输出结果
|
二、对word文档进行编辑
在对word文档编辑之前需要导入Document模块如下:
1 2 3 4 | from docx import Document
Doc = Document()
解释:from从docx这个文件中,导入一个叫Document的一个东西,Document是文档的意思,所以它是对word文档进行操作的一个玩意。
在下面Doc = Document()可以理解为 Document就是一个类,这个操作也就是实例化的过程,生成对象为:Doc
|
那Document实例化了一个Object叫Doc,那么Doc肯定会有很多的方法,这些方法就是对word文档进行操作的方法如下:
1 2 3 4 | Doc.add_heading( "Python是什么东西???" )
<docx.text.paragraph.Paragraph at 0x28033582e48>
# Doc.add_heading 意思是添加一个叫做heading的一个东西在这里heading指的是标题的意思,也就是添加一个标题叫做python是什么
东西???运行完成后它会生成一个对象。
|
2.1、添加一个段落(paragraph)
1 2 | Doc.add_paragraph( "Python是一种面向对象的编程语言~~~" ) # 在这里paragraph指的就是一个段落的意思
<docx.text.paragraph.Paragraph at 0x280335a17b8>
|
2.2、添加多个段落(paragraph)
1 2 | Doc.add_paragraph( "Python " )
Doc.add_paragraph( "Python 对word进行操作" )
|
2.3、如何查看word文档?
写完之后我们想要查看文档,首先需要保存,如下:
1 | Doc.save( "Python_word.docx" ) # save:保存为名字叫Python_word.docx的文件
|
相关推荐:《Python教程》
三、如何添加一级标题,二级标题,三级标题…?
3.1、添加一级标题
1 2 3 4 5 6 | Doc = Document()
Doc.add_heading( "这是一级标题" ,level=1)
Doc.add_heading( "这是二级标题" ,level=2)
Doc.add_heading( "这是三级标题" ,level=3)
Doc.add_heading( "这是四级标题" ,level=4)
Doc.save( "标题.docx" )
|
四、如何添加一个副级标题?
1 2 3 | Doc.add_heading( "这是一个一级标题" ,level=1)
Doc.add_paragraph( "这是一个副标题" , "Subtitle" )
Doc.save( "副标题.docx" )
|
五、查看已有的样式
5.1、查看paragraph有哪些样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from docx.enum.style import WD_STYLE_TYPE
for i in Doc.styles:
if i.type == WD_STYLE_TYPE.PARAGRAPH:
print (i.name)
>>>
Normal
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Heading 7
Heading 8
Heading 9
No Spacing
Title
Subtitle
List Paragraph
Body Text
Body Text 2
Body Text 3
List
List 2
List 3
List Bullet
List Bullet 2
List Bullet 3
List Number
List Number 2
List Number 3
List Continue
List Continue 2
List Continue 3
macro
Quote
Caption
Intense Quote
TOC Heading
|
5.2、查看文字有哪些样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from docx.enum.style import WD_STYLE_TYPE
for i in Doc.styles:
if i.type == WD_STYLE_TYPE.CHARACTER:
print (i.name)
>>>
Default Paragraph Font
Heading 1 Char
Heading 2 Char
Heading 3 Char
Title Char
Subtitle Char
Body Text Char
Body Text 2 Char
Body Text 3 Char
Macro Text Char
Quote Char
Heading 4 Char
Heading 5 Char
Heading 6 Char
Heading 7 Char
Heading 8 Char
Heading 9 Char
Strong
Emphasis
Intense Quote Char
Subtle Emphasis
Intense Emphasis
Subtle Reference
Intense Reference
Book Title
|
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from docx import Document
%cd D:YanZan_python2018word
Docx = Document()
Docx.add_heading( "这是一个一级标题" ,level=1)
Docx.add_paragraph( "这是一个副级标题" , "Title" )
A = Docx.add_paragraph( "My name is aaa" )
A.add_run( "我学习的很快乐,啊哈哈哈哈哈,非常好 Good!!!" )
Docx.add_heading( "这是一个二级标题" ,level=2)
A = Docx.add_paragraph( "这个是二级标题的内容呀" )
B = A.add_run( "二级标题里面的正文 继续添加!!!!!!!" )
B.font.bold = True # 同时我要对这些正文进行加粗~~~~
B.font.size = (20)
Docx.add_heading( "我爱学习Python以下就是python的logo呀" ,level=3)
Docx.add_picture( "1.png" )
Docx.add_table(rows=5, cols=5)
Docx.save( "Python.docx" )
|
效果如下:
