PowerPointファイルからテキストデータを抽出することで、テキストマイニングや文書ファイルの検索といった応用が可能になります。
本記事では、pptx形式のPowerPointファイルからテキストデータを抽出するPythonスクリプトについて、三種類の手法を比較して紹介します。
python-pptxを使用する
「python powerpoint テキスト 抽出」などで検索してよくヒットするのが、「python-pptx」を使用する方法です。
python-pptxは、pptx形式のPowerPointファイルを操作できるpythonライブラリで、Pythonによるスライド作成の自動化などの記事をよく見かけますが、作成だけでなく読み取りも可能です。
注意点として、読み取り・書き込みパスワードや権限が設定されている場合は利用出来ないという問題があります。また、「x」のついていない方の.ppt形式のPowerPointファイルは開けないので、何らかの方法で.pptxに変換する必要があります。
例1:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pptx | |
def pptx2text(file_path): | |
texts = [] # 抽出したテキストデータを格納する空リスト | |
prs = pptx.Presentation(file_path) | |
# スライドごとにテキストデータを抽出する | |
for sld in prs.slides: | |
for shape in sld.shapes: | |
# shapeに含まれるテキストデータを抽出 | |
if shape.has_text_frame: | |
for text in shape.text.splitlines(): | |
texts.append(text) | |
# tableに含まれるテキストデータを抽出 | |
if shape.has_table: | |
for cell in shape.table.iter_cells(): | |
for text in cell.text.splitlines(): | |
texts.append(text) | |
return texts |
Pythonの標準ライブラリを使用する
Office 2007 以降で保存可能になった.docx, .xlsx, .pptx といった拡張子のOfficeファイルは、XMLファイルなどをzip圧縮したものです。したがって、「re」や「zipfile」、「xml.etree.ElementTree」といった標準ライブラリだけを用いて読み取ることができます。
python-pptxを使用する場合と同様に、読み取り・書き込みパスワードや権限が設定されている場合はzipが解凍できないため利用出来ないという問題があります。また、.ppt形式の場合も、zipファイルではないので開けません。
ライブラリを追加したくない場合などはこの手法が使えると思います。
例2:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import zipfile | |
import xml.etree.ElementTree as ET | |
def pptx2text(file_path): | |
namespaces = {'a':'http://schemas.openxmlformats.org/drawingml/2006/main'} | |
texts = [] | |
with zipfile.ZipFile(file_path, 'r') as pptx: | |
files = [name for name in pptx.namelist() if re.match('ppt/slides/slide\d+.xml', name)] | |
files = sorted(files, key=lambda x:int((re.search(r"[0-9]+", x)).group(0))) | |
for file in files: | |
root = ET.fromstring(pptx.read(file)) | |
text_nodes = root.findall('.//a:t', namespaces=namespaces) | |
for text_node in text_nodes: | |
text_node = ET.tostring(text_node, encoding='utf-8').decode('utf-8') | |
text_node = re.sub('<a:t>.*?</a:t>', '', text_node) | |
text_node = re.sub('<.*?>', '', text_node) | |
if text_node == '': | |
continue | |
texts.append(text_node) | |
return texts |
参考:
PowerPointファイルからのテキスト抽出
最近、社内での業務プロセス改善の一環で、pptx形式のPowerPointファイルからテキスト抽出を自動化し解析を行いました。 そこで今回は pptxファイルからテキストを抽出する方法につ…
pywin32(win32com)を使用する
VBAで操作することに慣れている場合は、細かいところまで手が届くので良いかと思います。この方法だと、PowerPointのアプリケーションを経由して情報が抽出されるので、PowerPointアプリにアカウント設定がされていれば、権限設定のある.pptx形式のみならず、.ppt形式のPowerPointファイルの場合も操作可能です。
問題として、WindowsやPowerPointアプリのインストール・設定が必要となるので、環境構築の手間がかかります。
例3:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32com.client | |
def pptx2text(file_path): | |
texts = [] | |
pptx = win32com.client.gencache.EnsureDispatch('PowerPoint.Application') | |
pptx.Visible = True # アプリが開く(0, Falseにするとエラー) | |
pptx.DisplayAlerts = False # 警告OFF | |
try: | |
pptx_file = pptx.Presentations.Open(file_path, True) # 読み取り専用で開く | |
slide_count = len(pptx_file.Slides) + 1 | |
for i in range(1, slide_count): | |
slides = pptx_file.Slides(i) | |
shape_num = slides.Shapes.Count + 1 | |
for j in range(1, shape_num): | |
try: | |
texts.append(slides.Shapes(j).TextFrame.TextRange.Text) | |
texts.append(slides.NotesPage.Shapes(j).TextFrame.TextRange.Text) | |
except: | |
pass | |
pptx_file.Close() | |
finally: | |
pptx.Quit() | |
return texts |
参考:
Python: Microsoft PowerPointファイル(*.ppt)のテキストデータ抽出 - pywin32, win32com - Yukun's Blog
- Python: Microsoft PowerPointファイル(*.ppt)のテキストデータ抽出 - pywin32, win32com
win32comを使用することで、.pptファイルを.pptxファイルやpdfに変換することもできるので、上記の手法などと組み合わせて使うというのも一つの手かもしれません。
ここまで紹介した例では、「ノート」の中身のテキスト抽出について触れていませんでしたが、やり方を紹介されている方を見つけたのでリンクを掲載させていただきます。
python-pptx を使って PowerPoint ファイルの「発表者ノート」を抽出する
PowerPoint を使ってプレゼンテーションをするときに,原稿や関連情報を「発表者ノート」に書いておく人は多いと思う.そこで Python と python-pptx を使って,自動的に「発表者ノート」を抽出できるようにした.Python 3.8.5 と python-pptx v0.6.18 を前提にする. github…
関連記事:
【Python】PDF内の表をExcelに変換する
0 件のコメント:
コメントを投稿