处理Word文档

简介:

利用python-docx模块,Python可以创建和修改Word文档,它带有.docx文件扩展名。运行pip install python-docx,可以安装该模块。

在导入pytho-docx模块时,需要执行import docx,而不是import python-docx。

和纯文本相比,.docx文件有很多结构。这些结构在python-docx中用3种不同的类型来表示。在最高一层,Document对象表示整个文档。Document对象包含一个Paragraph对象的列表,表示文档中的段落。每一个Paragraph对象都包含一个Run对象的列表。

Word文档中的文本不仅仅是字符串。它包含与之相关的字体、大小、颜色和其他样式信息。在Word中,样式是这些属性的集合。一个Run对象是相同样式文本的延续。当文本样式发生改变时,就需要一个新的Run对象。

1
2
3
4
5
6
7
8
9
10
c:\python\Scripts>pip3.6  install  python-docx
Collecting python-docx
   Downloading python-docx-0.8.6. tar .gz (5.3MB)
     100% |████████████████████████████████| 5.3MB 73kB /s
Collecting lxml>=2.3.2 (from python-docx)
   Downloading lxml-4.1.1-cp36-cp36m-win_amd64.whl (3.5MB)
     100% |████████████████████████████████| 3.6MB 39kB /s
Installing collected packages: lxml, python-docx
   Running setup.py  install  for  python-docx ...  done
Successfully installed lxml-4.1.1 python-docx-0.8.6


读取Word文档

使用len()得到paragraphs的个数。

每个paragraph对象都有一个text属性,包含该段中的文本的字符串(没有样式信息)。

每个paragraph对象也有一个runs属性,它是run对象的列表。

run对象也有一个text属性,包含那个延续中的文本。

run表示的是不同样式的文本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
c:\python>python
Python  3.6 . 1  (v3. 6.1 : 69c0db5 , Mar  21  2017 18 : 41 : 36 ) [MSC v. 1900  64  bit (AMD64)] on win32
Type  "help" "copyright" "credits"  or  "license"  for  more information.
>>>  import  docx
>>> doc = docx.Document( 'Gettysburg Address.docx' )
>>>  len (doc.paragraphs)
4
>>> doc.paragraphs[ 0 ].text
'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'
>>> doc.paragraphs[ - 1 ].text
''
>>>  len (doc.paragraphs[ 0 ].runs)
1
>>> doc.paragraphs[ 0 ].runs[ 0 ].text
'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'
>>>


从文件中取得完整的文本

如果只关心word文档中的文本,不关心样式信息,就可以利用getText()函数。

它接受一个.docx文件名,返回其中文本的字符串。

可以通过在append()方法中增加空格,形成段落缩进。

1
2
3
4
5
6
7
8
9
>>>  import  docx
>>> doc = docx.Document( 'Gettysburg Address.docx' )
>>> fullText = []
>>>  for  para  in  doc.paragraphs:
...     fullText.append(para.text)
...
>>>  print (fullText)
[ 'Four score and seven years ago our fathers brought forth on this continent, ......hall not perish from the earth.' , '']
>>>


设置Paragraph和Run对象的样式

对于word文档,有3种类型的样式:段落样式可以应用于paragraph对象,字符样式可以应用于run对象,链接的样式可以应用于这两种对象。

在设置style属性时,不要在样式名称中使用空格。

如果对run对象应用链接的样式,需要在样式名称末尾加上‘char’。

在当前版本的python-docx(0.7.4)中,只能使用默认的word样式,以及打开的文件中已有的样式,不能创建新的样式。


写入Word文档

在添加完文本后,向Document对象的save()方法传入一个文件名字符串,将Document对象保存到文件。

可以用新的段落文本,再次调用add_paragraph()方法添加段落。

1
2
3
4
5
>>>  import  docx
>>> doc = docx.Document( 'Gettysburg Address.docx' )
>>> doc.add_paragraph( 'Hello world!' )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5DD45B70 >
>>> doc.save( 'newdoc.docx' )

效果:

QQ图片20171223235021.png


如果要在已有段落的末尾添加文本,可以调用paragraph对象的add_run方法,向它传入一个字符串。

add_paragraph()和add_run()都接受可选的第二个参数,它是表示Paragraph或Run对象样式的字符串。

1
2
3
4
5
6
7
8
9
>>>  import  docx
>>> doc = docx.Document( 'Gettysburg Address.docx' )
>>> doc.add_paragraph( 'Hello world!' )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5D57ABE0 >
>>> paraObj1 = doc.add_paragraph( 'This is a second paragraph.' )
>>> paraObj2 = doc.add_paragraph( 'This is a yet another paragraph.' )
>>> paraObj1.add_run( 'This text is being added to the second paragraph.' )
<docx.text.run.Run  object  at  0x0000019B5DD5B940 >
>>> doc.save( 'newdoc2.docx' )

效果:

QQ图片20171223235547.png


添加标题

调用add_heading()将添加一个段落,并使用一种标题样式。

add_heading()的参数,是一个标题文本的字符串,以及一个从0-4的整数。

整数0表示标题是Title样式,这用于文档的顶部。

整数1-4是不同的标题层次,1是主要的标题,4是最低层的子标题。

add_heading()返回一个Paragraph对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
>>>  import  docx
>>> doc = docx.Document()
>>> doc.add_heading( 'Header 0' , 0 )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5E1740F0 >
>>> doc.add_heading( 'Header 1' , 1 )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5E1740B8 >
>>> doc.add_heading( 'Header 2' , 2 )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5E174978 >
>>> doc.add_heading( 'Header 3' , 3 )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5E174198 >
>>> doc.add_heading( 'Header 4' , 4 )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5E1740F0 >
>>> doc.save( 'headings.docx' )

效果:

QQ图片20171224000459.png


添加换行符和换页符

要添加换行符,可以在run对象上调用add_break()方法。

要添加换页符,可以将docx.text.WD_BREAK.PAGE作为唯一的参数,传递给add_break()。

1
2
3
4
5
6
7
8
>>>  import  docx
>>> doc = docx.Document()
>>> doc.add_paragraph( 'This is on the first page!' )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5DD5B400 >
>>> doc.paragraphs[ 0 ].runs[ 0 ].add_break()
>>> doc.add_paragraph( 'This is the new text!' )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5DD5B400 >
>>> doc.save( 'break.docx' )

效果:

QQ图片20171224001311.png


添加图像

Document对象有一个add_picture()方法,在文档末尾添加图像。

1
2
3
4
5
6
7
>>>  import  docx
>>> doc = docx.Document()
>>> doc.add_paragraph( 'This is on the first page!' )
<docx.text.paragraph.Paragraph  object  at  0x0000019B5E189080 >
>>> doc.add_picture( '123.jpg' ,width = docx.shared.Inches( 1 ),height = docx.shared.Cm( 4 ))
<docx.shape.InlineShape  object  at  0x0000019B5E1899E8 >
>>> doc.save( 'picture.docx' )

效果:

QQ图片20171224002059.png









本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/2053946,如需转载请自行联系原作者
相关文章
|
11月前
|
Python
Word 批量转 PDF
Word 批量转 PDF
168 0
Word 批量转 PDF
word论文排版操作
word论文排版操作
94 0
|
存储 XML 自然语言处理
Word操作与应用
Word操作与应用
144 0
|
XML 前端开发 小程序
PDF转Word完全指南:3大方法满足各种场景!
还不知道PDF怎么转Word吗,本文将提供完整的PDF转Word方案,包括离线、在线或者SDK API等各种方式,总有一款满足您的需求。
727 0
Word - Word 文档如何切换简体字和繁体字?
Word - Word 文档如何切换简体字和繁体字?
132 0
Word - Word 文档如何切换简体字和繁体字?
|
Web App开发 测试技术 API
|
Web App开发 测试技术 API
|
安全 网络安全 数据安全/隐私保护