天天看點

處理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

<code>c:\python\Scripts&gt;pip3.6 </code><code>install</code> <code>python-docx</code>

<code>Collecting python-docx</code>

<code>  </code><code>Downloading python-docx-0.8.6.</code><code>tar</code><code>.gz (5.3MB)</code>

<code>    </code><code>100% |████████████████████████████████| 5.3MB 73kB</code><code>/s</code>

<code>Collecting lxml&gt;=2.3.2 (from python-docx)</code>

<code>  </code><code>Downloading lxml-4.1.1-cp36-cp36m-win_amd64.whl (3.5MB)</code>

<code>    </code><code>100% |████████████████████████████████| 3.6MB 39kB</code><code>/s</code>

<code>Installing collected packages: lxml, python-docx</code>

<code>  </code><code>Running setup.py </code><code>install</code> <code>for</code> <code>python-docx ... </code><code>done</code>

<code>Successfully installed lxml-4.1.1 python-docx-0.8.6</code>

讀取Word文檔

使用len()得到paragraphs的個數。

每個paragraph對象都有一個text屬性,包含該段中的文本的字元串(沒有樣式資訊)。

每個paragraph對象也有一個runs屬性,它是run對象的清單。

run對象也有一個text屬性,包含那個延續中的文本。

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

11

12

13

14

15

16

<code>c:\python&gt;python</code>

<code>Python </code><code>3.6</code><code>.</code><code>1</code> <code>(v3.</code><code>6.1</code><code>:</code><code>69c0db5</code><code>, Mar </code><code>21</code> <code>2017</code><code>, </code><code>18</code><code>:</code><code>41</code><code>:</code><code>36</code><code>) [MSC v.</code><code>1900</code> <code>64</code> <code>bit (AMD64)] on win32</code>

<code>Type</code> <code>"help"</code><code>, </code><code>"copyright"</code><code>, </code><code>"credits"</code> <code>or</code> <code>"license"</code> <code>for</code> <code>more information.</code>

<code>&gt;&gt;&gt; </code><code>import</code> <code>docx</code>

<code>&gt;&gt;&gt; doc</code><code>=</code><code>docx.Document(</code><code>'Gettysburg Address.docx'</code><code>)</code>

<code>&gt;&gt;&gt; </code><code>len</code><code>(doc.paragraphs)</code>

<code>4</code>

<code>&gt;&gt;&gt; doc.paragraphs[</code><code>0</code><code>].text</code>

<code>'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.'</code>

<code>&gt;&gt;&gt; doc.paragraphs[</code><code>-</code><code>1</code><code>].text</code>

<code>''</code>

<code>&gt;&gt;&gt; </code><code>len</code><code>(doc.paragraphs[</code><code>0</code><code>].runs)</code>

<code>1</code>

<code>&gt;&gt;&gt; doc.paragraphs[</code><code>0</code><code>].runs[</code><code>0</code><code>].text</code>

<code>&gt;&gt;&gt;</code>

從檔案中取得完整的文本

如果隻關心word文檔中的文本,不關心樣式資訊,就可以利用getText()函數。

它接受一個.docx檔案名,傳回其中文本的字元串。

可以通過在append()方法中增加空格,形成段落縮進。

<code>&gt;&gt;&gt; fullText</code><code>=</code><code>[]</code>

<code>&gt;&gt;&gt; </code><code>for</code> <code>para </code><code>in</code> <code>doc.paragraphs:</code>

<code>...     fullText.append(para.text)</code>

<code>...</code>

<code>&gt;&gt;&gt; </code><code>print</code><code>(fullText)</code>

<code>[</code><code>'Four score and seven years ago our fathers brought forth on this continent, ......hall not perish from the earth.'</code><code>, '']</code>

設定Paragraph和Run對象的樣式

對于word文檔,有3種類型的樣式:段落樣式可以應用于paragraph對象,字元樣式可以應用于run對象,連結的樣式可以應用于這兩種對象。

在設定style屬性時,不要在樣式名稱中使用空格。

如果對run對象應用連結的樣式,需要在樣式名稱末尾加上‘char’。

在目前版本的python-docx(0.7.4)中,隻能使用預設的word樣式,以及打開的檔案中已有的樣式,不能建立新的樣式。

寫入Word文檔

在添加完文本後,向Document對象的save()方法傳入一個檔案名字元串,将Document對象儲存到檔案。

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

<code>&gt;&gt;&gt; doc.add_paragraph(</code><code>'Hello world!'</code><code>)</code>

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5DD45B70</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.save(</code><code>'newdoc.docx'</code><code>)</code>

效果:

如果要在已有段落的末尾添加文本,可以調用paragraph對象的add_run方法,向它傳入一個字元串。

add_paragraph()和add_run()都接受可選的第二個參數,它是表示Paragraph或Run對象樣式的字元串。

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5D57ABE0</code><code>&gt;</code>

<code>&gt;&gt;&gt; paraObj1</code><code>=</code><code>doc.add_paragraph(</code><code>'This is a second paragraph.'</code><code>)</code>

<code>&gt;&gt;&gt; paraObj2</code><code>=</code><code>doc.add_paragraph(</code><code>'This is a yet another paragraph.'</code><code>)</code>

<code>&gt;&gt;&gt; paraObj1.add_run(</code><code>'This text is being added to the second paragraph.'</code><code>)</code>

<code>&lt;docx.text.run.Run </code><code>object</code> <code>at </code><code>0x0000019B5DD5B940</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.save(</code><code>'newdoc2.docx'</code><code>)</code>

添加标題

調用add_heading()将添加一個段落,并使用一種标題樣式。

add_heading()的參數,是一個标題文本的字元串,以及一個從0-4的整數。

整數0表示标題是Title樣式,這用于文檔的頂部。

整數1-4是不同的标題層次,1是主要的标題,4是最低層的子标題。

add_heading()傳回一個Paragraph對象。

<code>&gt;&gt;&gt; doc</code><code>=</code><code>docx.Document()</code>

<code>&gt;&gt;&gt; doc.add_heading(</code><code>'Header 0'</code><code>,</code><code>0</code><code>)</code>

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5E1740F0</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.add_heading(</code><code>'Header 1'</code><code>,</code><code>1</code><code>)</code>

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5E1740B8</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.add_heading(</code><code>'Header 2'</code><code>,</code><code>2</code><code>)</code>

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5E174978</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.add_heading(</code><code>'Header 3'</code><code>,</code><code>3</code><code>)</code>

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5E174198</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.add_heading(</code><code>'Header 4'</code><code>,</code><code>4</code><code>)</code>

<code>&gt;&gt;&gt; doc.save(</code><code>'headings.docx'</code><code>)</code>

添加換行符和換頁符

要添加換行符,可以在run對象上調用add_break()方法。

要添加換頁符,可以将docx.text.WD_BREAK.PAGE作為唯一的參數,傳遞給add_break()。

<code>&gt;&gt;&gt; doc.add_paragraph(</code><code>'This is on the first page!'</code><code>)</code>

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5DD5B400</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.paragraphs[</code><code>0</code><code>].runs[</code><code>0</code><code>].add_break()</code>

<code>&gt;&gt;&gt; doc.add_paragraph(</code><code>'This is the new text!'</code><code>)</code>

<code>&gt;&gt;&gt; doc.save(</code><code>'break.docx'</code><code>)</code>

添加圖像

Document對象有一個add_picture()方法,在文檔末尾添加圖像。

<code>&lt;docx.text.paragraph.Paragraph </code><code>object</code> <code>at </code><code>0x0000019B5E189080</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.add_picture(</code><code>'123.jpg'</code><code>,width</code><code>=</code><code>docx.shared.Inches(</code><code>1</code><code>),height</code><code>=</code><code>docx.shared.Cm(</code><code>4</code><code>))</code>

<code>&lt;docx.shape.InlineShape </code><code>object</code> <code>at </code><code>0x0000019B5E1899E8</code><code>&gt;</code>

<code>&gt;&gt;&gt; doc.save(</code><code>'picture.docx'</code><code>)</code>

本文轉自Grodd51CTO部落格,原文連結:http://blog.51cto.com/juispan/2053946,如需轉載請自行聯系原作者