金子邦彦研究室人工知能ChatGPT の利用パワーポイント (pptx) をテキストファイルに変換(Python 3.10 対応)(Python を使用)(Windows 上)

パワーポイント (pptx) をテキストファイルに変換(Python 3.10 対応)(Python を使用)(Windows 上)

要約】 このページのPythonプログラムは,ユーザがGUIを通じてPowerPoint(.pptx)ファイルを選択し,それをテキストファイルに変換する.具体的には,各スライドのタイトルと段落のテキストを抽出し,全角カンマと全角ピリオドに句読点を置き換える.結果は元のファイルと同じ名前の.txtファイルとして保存される.

目次

  1. 前準備
  2. パワーポイント (pptx) をテキストファイルに変換するプログラム(Python を使用)

前準備

Python のインストール(Windows 上)

サイト内の関連ページ

関連する外部ページ

Python の公式ページ: https://www.python.org/

python-pptx のインストール

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

    python -m pip install -U --ignore-installed pip
    python -m pip install -U python-pptx
    

パワーポイント (pptx) をテキストファイルに変換するプログラム

このプログラムは,GUIを使ってユーザがPowerPoint(.pptx形式)のファイルを選択し,その内容をテキストファイルに変換する.

以下に,このプログラムの主な機能である.

  1. Windows で,コマンドプロンプトを実行
  2. エディタを起動
    cd %HOMEPATH%
    notepad pptx2txt.py
    
  3. エディタで,次のプログラムを保存
    import collections 
    import collections.abc
    from pptx import Presentation
    import os
    import tkinter as tk
    from tkinter import filedialog
    
    def pptx_to_text(fpath):
        try:
            p = Presentation(fpath)
        except Exception as e:
            print(f"Failed to load {fpath}: {str(e)}")
            return
    
        text = ""
    
        # 各スライドを処理
        for slide in p.slides:
            title = None
            # タイトルがある場合は取得
            if slide.shapes.title:
                title = slide.shapes.title.text
    
            # タイトルをテキストに追加
            if title:
                text += f"タイトル:{title}\n\n"
    
            # 各スライド内の要素を処理
            for shape in slide.shapes:
                if shape.has_text_frame:
                    # 各テキストフレームを処理
                    for paragraph in shape.text_frame.paragraphs:
                        paragraph_text = ' '.join(run.text for run in paragraph.runs)
                        paragraph_text = paragraph_text.strip().replace("。", ".").replace("、", ",")  # 前後の空白を除去.置換
                        if paragraph_text:  # テキストが存在する場合のみ追加
                            text += paragraph_text + '\n'
    
            text += '\n'  # スライドの区切りに改行を追加
    
        # テキストファイルを作成・保存
        try:
            # 出力ファイル名を生成
            basename = os.path.basename(fpath)
            filename, _ = os.path.splitext(basename)
            output_fpath = os.path.join(os.path.dirname(fpath), f"{filename}.txt")
    
            with open(output_fpath, "w", encoding="utf-8") as text_file:
                text_file.write(text)
        except Exception as e:
            print(f"Failed to write to {output_fpath}: {str(e)}")
    
    
    root = tk.Tk()
    root.withdraw()
    
    fpaths = filedialog.askopenfilenames(filetypes=[('PowerPoint files', '*.pptx')])
    
    for fpath in root.tk.splitlist(fpaths):
        full_path = os.path.abspath(fpath)
        print("Processing file: ", full_path)
        if not full_path.endswith(".pptx"):
            print(f"Skipped {full_path} because it's not a .pptx file.")
            continue
        try:
            pptx_to_text(full_path)
        except Exception as e:
            print(f"Failed to process {full_path}. Exception: {str(e)}")
    
    print("Processing completed.")
    
  4. Python プログラムの実行

    Python プログラムの実行

    Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.

    Python のまとめ: 別ページ »にまとめ

    プログラムを pptx2txt.pyのようなファイル名で保存したので, 「python pptx2txt.py」のようなコマンドで行う.

    python pptx2txt.py 
    

    選択画面では,ファイルは複数選択可能である.

    [image]

    結果は,同じディレクトリに.txt形式のファイルとして保存される.ファイル名は,元の.pptxファイルと同じ名前が使用される.

    [image]