0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

複数のdocxファイルを一括でPDFに変換するPythonコード

0
Last updated at Posted at 2026-03-11

事前準備

事前にライブラリを用意する。
コマンドプロンプトに入力する。

cmd
pip install docx2pdf

使い方

  1. pythonの実行環境を用意する。VSCodeもあると良い。
  2. 下のプログラムを記載した .py ファイルを任意の場所におく。
  3. プログラム下部の YOUR_FOLDER_NAME を、対象の docx があるフォルダのパスに変更する。
  4. プログラムを実行する。(VSCodeで実行すると、ログが出て分かりやすい。)

プログラム

python
import os
from docx2pdf import convert

def convert_all_docx_to_pdf(folder_path):
    if not os.path.isdir(folder_path):
        print(f"指定されたパスはフォルダではありません: {folder_path}")
        return

    print(f"フォルダ内の.docxファイルをPDFに変換中: {folder_path}")
    try:
        convert(folder_path)
        print("変換が完了しました。")
    except Exception as e:
        print(f"変換中にエラーが発生しました: {e}")

# フォルダのパスを指定(例: "C:\Users\YourName\Documents\docx_files")
folder_path = r"YOUR_FOLDER_NAME"
convert_all_docx_to_pdf(folder_path)

注意点

・フォルダパスの前にある r を削除すると、\が誤動作して動かない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?