1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python入門 第18章】PDF / Word / PowerPoint操作入門📄オフィス文書もPythonで自動化🐍✨

Last updated at Posted at 2025-08-16

【Python入門 第18章】PDF / Word / PowerPoint操作入門📄オフィス文書もPythonで自動化🐍✨

こんにちは、 PythonTimesの蛇ノ目いろは です🌿
今回は、Pythonで PDF / Word / PowerPoint を読み書き・編集する方法を紹介するよ!


🗂️ オフィス文書もPythonで操作できる!

Pythonで操作できるファイルは、CSVやExcelだけじゃない!

  • PDFからテキスト抽出
  • Wordファイルの自動生成
  • PowerPointスライドの自動作成
    もぜんぶ可能✨

📄 PDFの操作(PyPDF2)

🔧 インストール

pip install PyPDF2

📖 テキスト抽出

from PyPDF2 import PdfReader

reader = PdfReader("sample.pdf")
for page in reader.pages:
    print(page.extract_text())

📝 Wordの操作(python-docx)

🔧 インストール

pip install python-docx

📄 Wordファイルを作成

from docx import Document

doc = Document()
doc.add_heading("レポートタイトル", level=1)
doc.add_paragraph("これは自動生成された段落です。")
doc.save("output.docx")

📊 PowerPointの操作(python-pptx)

🔧 インストール

pip install python-pptx

📈 スライド作成

from pptx import Presentation

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
title.text = "自動生成スライド"

subtitle = slide.placeholders[1]
subtitle.text = "Pythonで作りました!"

prs.save("output.pptx")

✍️ PEP8の補足:文書操作の関数分割

  • 例えば以下のように処理を関数化すると再利用しやすくなるよ👇
def create_word_report(title, body, filename):
    doc = Document()
    doc.add_heading(title, level=1)
    doc.add_paragraph(body)
    doc.save(filename)

📌 まとめ

  • PythonでPDF / Word / PowerPointを 自動生成・編集 できる!
  • ライブラリ:
    • PDF → PyPDF2 - Word →python-docx- PPT → python-pptx
  • レポートや定型資料の 業務自動化に最適✨ ** - PEP8に沿って ** 関数やファイルを整理 しよう🐍

次回は、
「第19章:社内業務でのPython活用例」 をお届け予定🏢
実際の業務シーンでどんなふうにPythonが使えるのか、具体例で紹介するよ!

フォローしてくれたらうれしいな🐍📄

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?