0
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でPDFにページを追加・インポート・コピー・移動・削除する方法

Posted at

日常のオフィス業務や自動化タスクでは、PDFファイルのページ単位での編集が必要になることがあります。例えば、空白ページの追加、既存ページのコピー、他のPDFからのページのインポート、不要なページの削除などです。Pythonを使えば、Adobe Acrobatに依存せず、これらの操作を簡単に実現できます。

この記事では、よくあるシナリオを通じて、PythonでPDFページを操作する方法を解説します。

  • 空白ページの追加
  • 他のPDFからページをインポート
  • 特定ページの削除
  • 文書内でページをコピー
  • ページを新しい位置に移動

すべての例は Free Spire.PDF for Python を使用しており、以下のコマンドでインストールできます:

pip install spire.pdf.free

1. PDFに空白ページを追加する

from spire.pdf.common import *
from spire.pdf import *

document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")

# 文書末尾に新しいページを追加
new_page = document.Pages.Add(document.Pages.get_Item(0).Size)  # 1ページ目と同じサイズを使用

# 任意:新しいページにテキストを描画
# text_element = PdfTextWidget("これは新しく追加された空白ページです", PdfFont(PdfFontFamily.Helvetica, 12))
# text_element.Draw(new_page, PointF(50, 50))

document.SaveToFile("output_add_blank_page.pdf", FileFormat.PDF)
document.Close()
print("空白ページを追加しました。")

説明

  • document.Pages.Add() は文書の末尾に新しいページを追加し、そのページオブジェクトを返します。
  • 特定の位置にページを挿入したい場合は、document.Pages.Insert(index) を使用します。
  • PdfTextWidget を使うことで、新しいページにタイトルや注釈を描画できます。

結果例:

2025-10-11_175212.png


2. 他のPDFからページをインポートする

from spire.pdf.common import *
from spire.pdf import *

# 目標PDFとソースPDFを読み込む
target_document = PdfDocument()
target_document.LoadFromFile("G:/Documents/Sample53.pdf")

source_document = PdfDocument()
source_document.LoadFromFile("G:/Documents/Sample89.pdf")

# ソースPDFの1ページ目を目標PDFの末尾にインポート
target_document.InsertPage(source_document, 0)

# 全ページをインポートしたい場合
# for i in range(source_document.Pages.Count):
#     target_document.InsertPage(source_document, i)

target_document.SaveToFile("output_import_page.pdf", FileFormat.PDF)
target_document.Close()
source_document.Close()
print("ページをソースPDFからインポートしました。")

説明

  • InsertPage(source_document, page_index) は、指定したページを別のPDFに挿入します。
  • ソースPDFが複数ページの場合は、ループで全ページをインポート可能です。
  • この方法は複数のPDFを1つに結合する際に便利です。

結果例:

2025-10-11_175322.png


3. PDFからページを削除する

from spire.pdf.common import *
from spire.pdf import *

document = PdfDocument()
document.LoadFromFile("input.pdf")

# 2ページ目(インデックス1)を削除
if document.Pages.Count > 1:
    document.Pages.RemoveAt(1)

document.SaveToFile("output_delete_page.pdf", FileFormat.PDF)
document.Close()
print("ページを削除しました。")

説明

  • RemoveAt(index) は指定したインデックスのページを削除します。
  • インデックスは0から始まります。1ページ目が0、2ページ目が1です。
  • ページ削除後は必ずファイルを保存して変更を反映します。

この方法は、表紙、空白ページ、広告ページなど不要なページを削除するのに適しています。

結果例:

2025-10-11_175302.png


4. 文書内でページをコピーする

from spire.pdf.common import *
from spire.pdf import *

document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")

# 1ページ目(インデックス0)をコピー
if document.Pages.Count > 0:
    document.InsertPage(document, 0)

document.SaveToFile("output_copy_page_within_doc.pdf", FileFormat.PDF)
document.Close()
print("ページを文書内でコピーしました。")

説明

  • InsertPage(document, page_index) は、同一文書内の指定ページを末尾にコピーします。
  • テンプレートページや繰り返しページを作成する場合に便利です。
  • 特定位置に挿入したい場合は、挿入位置パラメータを指定できます。

結果例:

2025-10-11_175240.png


5. ページを新しい位置に移動する

from spire.pdf.common import *
from spire.pdf import *

document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")

if document.Pages.Count > 1:
    temp_path = "temp_page.pdf"

    # Step 1: 移動するページを単独PDFとしてエクスポート
    temp_doc = PdfDocument()
    temp_doc.InsertPage(document, 1, 0)
    temp_doc.SaveToFile(temp_path, FileFormat.PDF)
    temp_doc.Close()

    # Step 2: 元の文書から該当ページを削除
    document.Pages.RemoveAt(1)

    # Step 3: エクスポートしたページを再読み込み
    imported_doc = PdfDocument()
    imported_doc.LoadFromFile(temp_path)

    # Step 4: 新しい位置に挿入(例:文書の先頭)
    document.InsertPage(imported_doc, 0, 0)

document.SaveToFile("output_move_page.pdf", FileFormat.PDF)
document.Close()
print("ページを新しい位置に移動しました。")

説明

  • Spire.PDFは直接ページを移動する機能がないため、「エクスポート → 削除 → 挿入」の手順で実現します。
  • InsertPage(imported_doc, target_index, source_index) で任意の位置にページを挿入可能です。
  • ページ順序を調整する場合に柔軟で信頼性の高い方法です。

結果例:

2025-10-11_175341.png


6. 主要クラスとメソッドのまとめ

操作内容 メソッド / プロパティ 説明
空白ページ追加 Pages.Add() 文書末尾に新しいページを作成、ページサイズ指定可能
指定位置に挿入 Pages.Insert(index) 指定したインデックス位置にページを挿入
ページインポート InsertPage(source_doc, page_index) 他のPDFからページを現在の文書にインポート
ページ削除 Pages.RemoveAt(index) 指定したページを削除
ページコピー InsertPage(document, page_index) 文書内のページを末尾にコピー
ページ移動 「エクスポート → 削除 → 挿入」の組み合わせ ページ位置の変更を実現

7. まとめ

上記の例から、Spire.PDF for Python はPDFページ単位の操作をシンプルかつ強力に提供していることがわかります。
空白ページの追加、コピー、インポート、削除など、すべて数行のコードで実現可能です。

このプログラミング方式は、複数文書の編集、自動レポート生成、ファイル構造の整理などに特に便利です。
Adobe Acrobatをインストールせずとも、自分だけのPDF管理ツールを簡単に構築できます。

さらに詳しいPDF処理のチュートリアルは、Spire.PDF for Python公式ガイドをご参照ください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?