pdf轉圖片

freedragon發表於2024-12-06

1. 安裝 Release, 配置環境變數

Release 24.08.0-0

Latest
@github-actions github-actions released this Oct 7
v24.08.0-0
d155855
Merge pull request #71 from Fdawgs/patch-1

Poppler 24.08.0

2. 安裝 圖片庫

pip install pdf2image

3. 程式

from pdf2image import convert_from_path
import os

def pdf_to_images(pdf_path, output_folder, poppler_path):
    # 如果輸出資料夾不存在,則建立它
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 將PDF檔案轉換為影像列表,指定poppler路徑
    images = convert_from_path(pdf_path, poppler_path=poppler_path)

    # 儲存每個影像為一個檔案
    for i, image in enumerate(images):
        image_path = os.path.join(output_folder, f'page_{i + 1}.jpg')
        image.save(image_path, 'JPEG')

if __name__ == "__main__":
    # PDF檔案路徑
    pdf_file = "example.pdf"
    # 輸出資料夾路徑
    output_dir = "output_images"
    # poppler可執行檔案所在目錄路徑,根據你的實際安裝路徑修改
    poppler_path = r"D:\迅雷下載\Release-24.08.0-0\poppler-24.08.0\Library\bin"
    pdf_to_images(pdf_file, output_dir, poppler_path)

相關文章