處理Word文件


要讀取word文件,可使用python中的docx模組。 首先安裝docx,如下所示。 然後編寫一個程式,使用docx模組中的不同函式按段落讀取整個檔案。

使用以下命令將docx模組放入程式環境中。

 pip install docx

在下面的範例中,通過將每個行附加到段落並最終列印出所有段落文字來讀取word文件的內容。

import docx

def readtxt(filename):
    doc = docx.Document(filename)
    fullText = []
    for para in doc.paragraphs:
        fullText.append(para.text)
    return '\n'.join(fullText)

print (readtxt('path\yiibaispoint.docx'))

當執行上面的程式時,我們得到以下輸出 -

Yiibai Point originated from the idea that there exists a class of readers who respond 
better to online content and prefer to learn new skills at their own pace from the comforts 
of their drawing rooms. 

The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, 
we worked our way to adding fresh tutorials to our repository which now proudly flaunts 
a wealth of tutorials and allied articles on topics ranging from programming languages 
to web designing to academics and much more.

讀取個別段落

可以使用paragraph屬性從word文件中讀取特定段落。 在下面的例子中,唯讀取word文件中的第二段。

import docx

doc = docx.Document('path\Yiibaispoint.docx')
print len(doc.paragraphs)

print doc.paragraphs[2].text

當執行上面的程式時,我們得到以下輸出 -

The journey commenced with a single tutorial on HTML in 2006 and elated by the response 
it generated, we worked our way to adding fresh tutorials to our repository 
which now proudly flaunts a wealth of tutorials and allied articles on topics 
ranging from programming languages to web designing to academics and much more.