0
0

More than 1 year has passed since last update.

python-pptxを用いたセクション名の取得

Last updated at Posted at 2023-05-16

0.はじめに

Microsoft Officeは各企業において多用されており、日々ファイルが生産される一方、自動化ノウハウは未だにマイナーな存在です。今回は「パワーポイントのセクション名取得」という中でもマイナーな話題ですが、検索しても曖昧な話が多かったので自分用メモに投稿します。

1.前提と知っておくべきこと

1.Pythonによる自動化、python-pptx, bs4を使用する。
2.Powerpointファイルなどはunzipすることができ、その中身はxmlファイル群などで構成されている。
3.セクション名などはファイル内の/pptx/presentation.xmlに記述されている。
4.今回は製作者のgitも見てみましたが、目当ての変数にそのままアクセスできそうだったのでbs4で取得しました。

2.スクリプト例

section_name.py
import pptx
import bs4

path='/path-to-your-powepoint-file.pptx'
prs=pptx.Presentation(path)

#target xml is './pptx/presentation.xml'
package_parts = prs.part.package.iter_parts()
for part in package_parts:
    if part.partname.endswith("presentation.xml"):

# get soup, and get targets, which are written as attributes of 'p14'tag      
        _xml = part.blob.decode("utf-8")
        soup = bs4.BeautifulSoup(_xml, "xml")

        for section in soup.find_all('p14:section'):
            print(section['name'])

3.おわり

おわり。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0