Extracting content from Word documents allows us to use them for other operations, such as storing content in databases, importing content into other programs, for artificial intelligence training and creating other documents. Spire.Doc for Python makes it easy to extract text and images from Word documents without extensive copy-and-paste or complex coding. This article explains how to extract and save text and image content from a Word document using simple code.
Import Spire.Doc for Python
pip install Spire.Doc pip install plum-dispatch==1.7.4
Musterdokument
Extract text from Word document and write to TXT file
method of Spire.Doc for Python can retrieve all text in a Word document and return it as a string. We can write the returned string into a text file for storage. The steps are as follows:
Code Bespiel
Python
Copy from turtle import st from spire.doc import * from spire.doc.common import * def WriteAllText(fname:str,text:List[str]): fp = open(fname,"w") for s in text: fp.write(s) fp.close() inputFile = "Beispiel.docx" outputFile = "Extrahierter Text.txt" #Document-Objekt erstellen document = Document() #Word-Dokument laden document.LoadFromFile(inputFile) #Text aus Dokument abrufen text = document.GetText() #Text in Textdatei schreiben WriteAllText(outputFile, text) document.Close()
Extrahierter Text
Bilder aus Word-Dokument extrahieren und speichern
Code Bespiel
Python
Copy import queue from spire.doc import * from spire.doc.common import * import os outputPath = "Bilder/" inputFile = "Beispiel.docx" if not os.path.exists(outputPath): os.makedirs(outputPath) #Document-Objekt erstellen document = Document() #Word-Dokument laden document.LoadFromFile(inputFile) #Warteschlange erstellen und Dokumentenelemente hinzufügen nodes = queue.Queue() nodes.put(document) #Liste erstellen images = [] #Dokumentenelemente durchlaufen while nodes.qsize() > 0: node = nodes.get() for i in range(node.ChildObjects.Count): #Untergeordnetes Objekt des Dokumentenelements abrufen child = node.ChildObjects.get_Item(i) #Prüfen, ob es ein Bild ist if child.DocumentObjectType == DocumentObjectType.Picture: picture = child if isinstance(child, DocPicture) else None dataBytes = picture.ImageBytes #Zur Liste hinzufügen images.append(dataBytes) #Prüfen, ob es ein zusammengesetztes Objekt ist elif isinstance(child, ICompositeObject): #Zur Warteschlange hinzufügen nodes.put(child if isinstance(child, ICompositeObject) else None) #Bilder speichern for i, item in enumerate(images): fileName = "Bild-{}.png".format(i) with open(outputPath+fileName,'wb') as imageFile: imageFile.write(item) document.Close()
Extrahierte Bilder
Der extrahierte Text wird mit angehängten Bewertungsinformationen gespeichert. Sie können die Bewertungsinformationen direkt am Anfang des Textes löschen.
This is an introduction to using Spire.Doc for Python to extract text and images from Word documents. Spire.Doc for Python supports many other document operations. Check out the official website or join the Spire.Doc forum.
The above is the detailed content of Extract text and images from Word documents using Python. For more information, please follow other related articles on the PHP Chinese website!