Python - Przetwarzaj dokument Word
Aby przeczytać dokument tekstowy, korzystamy z modułu o nazwie docx. Najpierw instalujemy docx, jak pokazano poniżej. Następnie napisz program, który będzie używał różnych funkcji w module docx do odczytywania całego pliku po akapitach.
Używamy poniższego polecenia, aby pobrać moduł docx do naszego środowiska.
pip install docxW poniższym przykładzie czytamy zawartość dokumentu tekstowego, dołączając każdy z wierszy do akapitu i ostatecznie drukując cały tekst akapitu.
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\Tutorialspoint.docx'))Po uruchomieniu powyższego programu otrzymujemy następujący wynik -
Tutorials 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.Czytanie poszczególnych akapitów
Możemy odczytać określony akapit z dokumentu tekstowego za pomocą atrybutu akapity. W poniższym przykładzie czytamy tylko drugi akapit z dokumentu Word.
import docx
doc = docx.Document('path\Tutorialspoint.docx')
print len(doc.paragraphs)
print doc.paragraphs[2].textPo uruchomieniu powyższego programu otrzymujemy następujący wynik -
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.