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?

複数のSVGファイルをPDFファイルに変換する方法

Posted at

MacでSVGファイルを扱うにはGapplinを用いることが有名ですが、複数のSVGファイルをPDF化するにはいちいち保存しない解けないので面倒ですよね

そこで、Pythonで指定したフォルダ内のSVGファイルをPDFに変換するコードを書いてみました。

svg2pdf.py
import os
from tkinter import filedialog
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF

def convert_svg_to_pdf(input_folder):
    # 出力フォルダを作成
    output_folder = os.path.join(input_folder, "pdf_output")
    os.makedirs(output_folder, exist_ok=True)

    # 入力フォルダ内のすべてのSVGファイルを処理
    for filename in os.listdir(input_folder):
        if filename.lower().endswith('.svg'):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.pdf")

            try:
                # SVGファイルを読み込む
                drawing = svg2rlg(input_path)
                
                # SVGをPDFに変換して保存する
                renderPDF.drawToFile(drawing, output_path)
                print(f"変換成功: {filename}")
            except Exception as e:
                print(f"変換失敗: {filename} - エラー: {str(e)}")

# フォルダ選択ダイアログを表示
input_folder = filedialog.askdirectory(title="SVGファイルを含むフォルダを選択してください")

if input_folder:
    convert_svg_to_pdf(input_folder)
    print("変換プロセスが完了しました。")
else:
    print("フォルダが選択されませんでした。")

実行前に以下の、モジュールをインストールしておきましょう!

pip install svglib reportlab
brew install python-tk
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?