Skip to content

Instantly share code, notes, and snippets.

@sugikeitter
Last active February 6, 2025 01:10
Show Gist options
  • Save sugikeitter/4b9c0a63c22610dd2805bb2558404650 to your computer and use it in GitHub Desktop.
Save sugikeitter/4b9c0a63c22610dd2805bb2558404650 to your computer and use it in GitHub Desktop.
Extract pptx file's note and output text files.

使い方

概要

  • pptx ファイルの Note 部分を抽出して、<元のファイル名>.note.md を出力する
  • 例) sample01.pptx -> sample01.pptx.note.md

事前準備

  • Python (v3.9系)インストール or pyenv
    • python-pptx が v3.10以降に対応していないため
    • 必要あれば venv などの仮想環境を使用する
    • pyenv + venv 使用する場合
      • pyenv install 3.9
      • pyenv local 3.9
      • python3 --version
      • python3 -m venv .py39
      • source .py39/bin/activate
  • パッケージインストール
# venv 使用している場合
python3 -m pip install python-pptx

# バイナリインストールして python3.9 コマンドを設定している場合
python3.9 -m pip install python-pptx

export_title_from_pptx_to_md.py

準備

  • export_title_from_pptx_to_md.py 8 行目の pptx_folder_name = '/Users/xxx/yyy' に pptx ファイルがあるディレクトリを設定 (末尾の / は入れてはいけない)

  • ディレクトリ構成の例 (export_title_from_pptx_to_md.py の場所はどこでも良い)

.
└── PowerPointFiles
    ├── Sample01.pptx
    ├── Sample02.pptx
    └── Sample03.pptx

実行

python3.9 export_title_from_pptx_to_md.py
  • 実行後のディレクトリ構成
.
└── PowerPointFiles
    ├── Sample01.pptx
    ├── Sample02.pptx
    └── Sample03.pptx
    └── md
        ├── Sample01.md
        ├── Sample02.md
        └── Sample03.md

extract_pptx_note.py

準備

  • extract_pptx_note.py と同じディレクトリに以下のパスとファイル名で pptx ファイルを配置 (該当するもの全てが対象)
    • PowerPointFiles/*.pptx
    • PowerPointFiles/v*/*.pptx
  • ディレクトリ構成の例
.
├── PowerPointFiles
│   ├── Sample01.pptx
│   ├── Sample02.pptx
│   ├── v1.1
│   │   ├── Introduction.pptx
│   │   └── Closing.pptx
│   └── v2.0
│       ├── Introduction.pptx
│       └── Closing.pptx
└── extract_pptx_note.py

実行

python3.9 extract_pptx_note.py
  • 実行後のディレクトリ構成
.
├── PowerPointFiles
│   ├── Sample01.pptx
│   ├── Sample01.pptx.note.md
│   ├── Sample02.pptx
│   ├── Sample02.pptx.note.md
│   ├── v1.1
│   │   ├── Introduction.pptx
│   │   ├── Introduction.pptx.note.md
│   │   ├── Closing.pptx
│   │   └── Closing.pptx.note.md
│   └── v2.0
│   │   ├── Introduction.pptx
│   │   ├── Introduction.pptx.note.md
│   │   ├── Closing.pptx
│   │   └── Closing.pptx.note.md
└── extract_pptx_note.py
from glob import glob
import os
import re
import pptx # Until Python3.9 at 2023/03/20
pptx_folder_name = '/Users/xxx/yyy' # 末尾に / を入れてはいけない
pptx_file_paths = [f for f in glob(pptx_folder_name + '/*.pptx') if '~' not in f]
pptx_file_paths.extend([f for f in glob(pptx_folder_name + '/v*/*.pptx') if '~' not in f])
output_folder = pptx_folder_name + '/md'
def extract_pptx_title_to_md():
os.makedirs(output_folder, exist_ok=True)
# なぜかタイトル文字列に \x0B など制御文字が紛れ込んでいることがあるので、後ほど削除する
compile = re.compile('[\x00-\x1F\x7F]')
for pptx_file_path in pptx_file_paths:
p = pptx.Presentation(pptx_file_path)
print('File Path: {}'.format(pptx_file_path))
file_name = pptx_file_path[(len(pptx_folder_name)+1):-len('.pptx')]
print('# {}'.format(file_name))
output_path = output_folder + '/' + file_name + '.md'
# output_path = pptx_folder_name + '/md/' + file_name + '.md'
with open(output_path, mode='w', encoding='utf-8') as f:
# f.write('# {}'.format(pptx_file_path.removeprefix(pptx_folder_name).removesuffix('.pptx'))) # Python 3.9 or later
f.write('# {}\n\n'.format(pptx_file_path[(len(pptx_folder_name)+1):-len('.pptx')]))
f.write('## 概要\n\n')
for i, slide in enumerate(p.slides, start=1):
# note = slide.notes_slide
title = '__NON_TITLE__'
if slide.shapes.title:
title = slide.shapes.title.text
else:
if (len(slide.shapes.placeholders) >= 1 and slide.shapes.placeholders[1].text):
title = slide.shapes.placeholders[1].text
# title から制御文字を削除
t = compile.sub('', title)
print('### p.{} {}\n'.format(i, t))
f.write('### p.{} {}\n\n\n'.format(i, t))
# print(slide.notes_slide.notes_text_frame.text)
# f.write(slide.notes_slide.notes_text_frame.text + '\n')
if __name__ == '__main__':
extract_pptx_title_to_md()
from glob import glob
import re
import pptx # Until Python3.9 at 2023/03/20
pptx_file_names = [f for f in glob('./PowerPointFiles/*.pptx') if '~' not in f]
pptx_file_names.extend([f for f in glob('./PowerPointFiles/v*/*.pptx') if '~' not in f])
def extract_pptx_note_to_md():
"""
pptx から md
"""
# なぜかタイトル文字列に \x0B など制御文字が紛れ込んでいることがあるので、後ほど削除する
re_compile = re.compile('[\x00-\x1F\x7F]')
for pptx_file_name in pptx_file_names:
p = pptx.Presentation(pptx_file_name)
print('File Name: {}'.format(pptx_file_name))
print('# {}'.format(pptx_file_name[len('./PowerPointFiles/'):-len('.pptx')]))
# f.write('# {}'.format(pptx_file_name.removeprefix('./PowerPointFiles/').removesuffix('.pptx'))) # Python 3.9 or later
print('# {}'.format(pptx_file_name[len('./PowerPointFiles/'):-len('.pptx')]))
for i, slide in enumerate(p.slides, start=1):
title = '__NON_TITLE__'
if slide.shapes.title:
title = slide.shapes.title.text
else:
if (len(slide.shapes.placeholders) >= 1 and slide.shapes.placeholders[1].text):
title = slide.shapes.placeholders[1].text
# title から制御文字を削除
t = re_compile.sub('', title)
print('## Page: {}\n### {}\n'.format(i, t))
print('')
# print(len(slide.shapes.placeholders))
# for ph in slide.shapes.placeholders:
# print(ph.text)
print('')
if __name__ == '__main__':
extract_pptx_note_to_md()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment