Word 文書からコンテンツを抽出すると、コンテンツをデータベースに保存したり、他のプログラムにコンテンツをインポートしたり、人工知能のトレーニングや他の文書の作成など、他の操作に使用できるようになります。 Spire.Doc for Python を使用すると、大規模なコピー アンド ペーストや複雑なコーディングを行わなくても、Word 文書からテキストや画像を簡単に抽出できます。この記事では、簡単なコードを使用して Word 文書から テキストと画像コンテンツを抽出して保存する方法について説明します。
Python 用の Spire.Doc をインポートするこのツールを使用して Word 文書を編集するには、その前にそれをプロジェクトにインポートする必要があります。 Spire.Doc for Python 公式 Web サイトからダウンロードするか、pip を使用して直接インストールできます。コードは次のようになります:
pip install Spire.Doc pip install plum-dispatch==1.7.4
マスタードキュメント
Spire.Doc for Python の
Document.GetText() メソッドは、Word 文書内のすべてのテキストを取得し、文字列として返すことができます。返された文字列をテキスト ファイルに書き込んで保存できます。手順は次のとおりです。
- Document オブジェクトを作成します。
- Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。
- Document.GetText() メソッドを使用してドキュメントからテキストを取得します。 Den abgerufenen Text in eine Textdatei schreiben.
コードベシュピール
パイソン
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()
追加テキスト
Das Extrahieren von Bildern ist etwas komplexer、ob dessen untergeordnete Objekte Bilder enthalten:
- Document オブジェクトを作成します。
- Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。 Eine Warteschlange für zusammengesetzte Objekte erstellen und die Dokumentenelemente hinzufügen.
- Eine Liste zum Speichern der extrahierten Bilder erstellen.
- Die Dokumentenelemente durchlaufen と die untergeordneten Objekte jedes Knotens durchlaufen, um zu prüfen, ob es sich um ein zusammengesetztes Objekt oder Bildobjekt handelt.
- Prüfen, ob das untergeordnete Element ein Bildobjekt ist. Die Bilddaten extrahieren und zur Liste hinzufügen.
- Prüfen、ob das untergeordnete Element ein zusammengesetztes Objekt ist. Wenn ja, zur Warteschlange hinzufügen und weiter prüfen.
- Einen Ordner speichern のビルダー。
コードベシュピール
パイソン
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()
特別な写真
追加のテキスト情報を参照してください。テキストを参照してください。
これは、Spire.Doc for Python を使用して Word 文書からテキストと画像を抽出する方法の紹介です。 Spire.Doc for Python は、他の多くのドキュメント操作をサポートしています。公式 Web サイトをチェックするか、Spire.Doc フォーラムに参加してください。
以上がPython を使用して Word 文書からテキストと画像を抽出するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

WebStorm Mac版
便利なJavaScript開発ツール

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

ホットトピック









