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をマージする

Last updated at Posted at 2025-11-13

PythonでPDFファイルのマージ

Acrobat readerではマージはできないのでPythonで作ってみました。

ライブラリのインストール

pip install pypdf

マージプログラム

ちょっとめんどくさかったのでマージするファイルの情報などはリストに定義してください。
マージされたPDFファイルはoutput.pdfに出力されます。

pmerg.py
from pypdf import PdfReader, PdfWriter

# マージするファイルはここに指定、いくつあってもいいですよー
pdf_files = ['マージするファイル1.pdf', 'マージするファイル2.pdf']
output_filename = 'output.pdf'

# PdfWriterオブジェクトを作成
writer = PdfWriter()

try:
    # 各ファイルをReaderで読み込む
    for pdf_file in pdf_files:
        reader = PdfReader(pdf_file)
        
        # 読み込んだファイルの全ページをWriterに追加
        for page in reader.pages:
            writer.add_page(page)
            
    # 結合した内容をファイルに書き出す
    with open(output_filename, 'wb') as f:
        writer.write(f)
        
    print(f"成功: ファイルを '{output_filename}' として保存しました。")

finally:
    # Writerを閉じる
    writer.close()

あとがき

これでオンラインサービスはいらない!
機密性の高い文書もこのプログラムでローカルPCマージできます。

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?