2026年6月25日 星期四

只要點兩下,就能將input資料夾內所有PDF進行容量壓縮-版本02

系列文章:
01.python 不管何時何地,只要點兩下,資料夾內的所有pdf都會合併成一個pdf
https://skjhcreator.blogspot.com/2022/06/pythonpdfpdf.html
02.python 只要點兩下,分別對各資料夾內的pdf合併,合併後的檔名要讓人知道來自哪個資料夾 
https://skjhcreator.blogspot.com/2022/06/python-pdf.html
03. 只要點兩下,就能將一堆的Doc與Docx 轉成 PDF
03.https://skjhcreator.blogspot.com/2023/05/docdocx-pdf.html
04.只要點兩下,就能將一堆的JPG轉成一個PDF,並以JPG所在的資料夾名稱為PDF的檔名
04.https://skjhcreator.blogspot.com/2023/06/jpgpdfjpgpdf.html
05.只要點兩下,就能將放進input的一堆PDF轉成各自的WORD
05.https://skjhcreator.blogspot.com/2023/10/inputpdfword.html
06.只要點兩下,就能將放進input的一堆PDF轉成在ouput資料夾內的各自的WORD 
06.https://skjhcreator.blogspot.com/2023/10/inputpdfouputword.html
07.只要點兩下,就能夠將InputAndOutput資料夾底下的子子孫孫資料夾內所有Word通通轉成PDF
07.https://skjhcreator.blogspot.com/2024/09/word-word-pdf.html
08.只要點兩下,就可以將資料夾input內的所有Word通通轉成一個PDF
08.https://skjhcreator.blogspot.com/2025/07/inputwordpdf.html
09.只要點兩下兩次,就能依照設定拆分指定PDF頁數並合併成設定好的一個PDF
09.https://skjhcreator.blogspot.com/2025/09/pdfpdf.html
10.只要點兩下,就能將input資料夾內所有PDF進行容量壓縮-版本01
10.https://skjhcreator.blogspot.com/2026/04/inputpdf.html
11.只要點兩下,就能將input資料夾內所有PDF進行容量壓縮-版本02
11.https://skjhcreator.blogspot.com/2026/06/inputpdf-02.html

Just double-click to compress the file size of all PDFs in the input folder – Version 02.
        PDF 壓縮設定為:
screen = 最小檔案,適合螢幕閱讀
ebook = 一般文件,推薦使用
printer = 列印品質
prepress = 印刷品質
        由於版本01 將壓縮設定 固定在 ebook,也就是一般文件,推薦使用。當想要提高壓縮比,版本01就無法滿足。所以版本02就將壓縮設定寫進 setting.ini,若 setting.ini 不見了,版本02還可以生成。讓使用者可以修改 setting.ini,來滿足其需求。

PDF Compression Settings:

  • screen = Smallest file size, suitable for on-screen reading
  • ebook = Standard document quality, recommended for most uses
  • printer = Printer-quality output
  • prepress = Prepress quality for professional printing

In Version 01, the compression setting was fixed to ebook (standard document quality), which is recommended for general use. However, when a higher compression ratio is required, Version 01 cannot meet that need.

To address this limitation, Version 02 stores the compression setting in setting.ini. Users can modify the setting in setting.ini to suit their specific requirements. In addition, if setting.ini is missing, Version 02 can automatically regenerate it, ensuring the program continues to function properly.


下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):


方法:Python + Ghostscript
一、下載與安裝 Ghostscript
https://www.ghostscript.com/download/gsdnld.html 下載後,點兩下進行安裝

二、Python 程式碼
檔案:pyPdfCompress01.py
檔案內容:

import os
import subprocess
import shutil
import webbrowser
import sys
import argparse
import platform
import configparser

INPUT_DIR = "input"
OUTPUT_DIR = "output"
SETTINGS_FILE = "settings.ini"

QUALITY_SETTINGS = {
    "screen": "/screen",
    "ebook": "/ebook",
    "printer": "/printer",
    "prepress": "/prepress"
}


def create_default_settings():
    config_content =
"""
; ==========================================
; PDF 壓縮設定
; ==========================================
;
; 可用品質:
;
; screen   = 最小檔案,適合螢幕閱讀
; ebook    = 一般文件,推薦使用
; printer  = 列印品質
; prepress = 印刷品質
;
; 修改 quality 的值即可
;
; ==========================================

[default]
quality = ebook

; quality = screen
; quality = printer
; quality = prepress
"""

    with open(SETTINGS_FILE, "w", encoding="utf-8") as f:
        f.write(config_content)


def load_settings():
    if not os.path.exists(SETTINGS_FILE):
        create_default_settings()
        print(f"📄 已建立預設設定檔: {SETTINGS_FILE}")

    config = configparser.ConfigParser()
    config.read(SETTINGS_FILE, encoding="utf-8")

    quality = config.get(
        "default",
        "quality",
        fallback="ebook"
    ).lower()

    if quality not in QUALITY_SETTINGS:
        print(
            f"⚠ settings.ini 中的 quality='{quality}' 無效"
        )
        print("⚠ 已自動改用 ebook")
        quality = "ebook"

    return quality


def check_ghostscript():
    gs = shutil.which("gswin64c") or shutil.which("gs")

    if gs is None:
        print("❌ [未安裝 Ghostscript]")

        if platform.system() == "Windows":
            print("👉 請下載 Windows 版本 (gswin64)")
        else:
            print("👉 請下載對應系統版本")

        print("👉 即將開啟下載頁面...")

        webbrowser.open(
            "https://www.ghostscript.com/download/gsdnld.html"
        )

        print("⚠ 若已安裝,請確認 Ghostscript 已加入 PATH")

        sys.exit(1)

    return gs


def compress_pdf(
    gs_command,
    input_path,
    output_path,
    quality
):
    cmd = [
        gs_command,
        "-sDEVICE=pdfwrite",
        "-dCompatibilityLevel=1.4",
        f"-dPDFSETTINGS={QUALITY_SETTINGS[quality]}",
        "-dNOPAUSE",
        "-dQUIET",
        "-dBATCH",
        f"-sOutputFile={output_path}",
        input_path
    ]

    result = subprocess.run(
        cmd,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.PIPE,
        text=True
    )

    if result.returncode != 0:
        raise RuntimeError(result.stderr)


def process_file(
    gs_command,
    file,
    quality
):
    input_path = os.path.join(INPUT_DIR, file)

    name, ext = os.path.splitext(file)

    output_path = os.path.join(
        OUTPUT_DIR,
        f"{name}_compressed{ext}"
    )

    compress_pdf(
        gs_command,
        input_path,
        output_path,
        quality
    )

    original_size = os.path.getsize(input_path)
    compressed_size = os.path.getsize(output_path)

    print(f"✔ {file}")
    print(f"   原始: {original_size / 1024:.2f} KB")
    print(f"   壓縮: {compressed_size / 1024:.2f} KB")

    if compressed_size >= original_size:
        print("   ⚠ 壓縮後反而變大,已刪除壓縮檔\n")
        os.remove(output_path)
        return original_size, original_size

    reduction = original_size - compressed_size
    ratio = reduction / original_size * 100

    print(f"   減少: {reduction / 1024:.2f} KB")
    print(f"   壓縮率: {ratio:.2f}%\n")

    return original_size, compressed_size


def main():
    default_quality = load_settings()

    parser = argparse.ArgumentParser(
        description="PDF 批次壓縮工具"
    )

    parser.add_argument(
        "--quality",
        choices=QUALITY_SETTINGS.keys(),
        default=default_quality,
        help=f"壓縮品質 (預設: {default_quality})"
    )

    args = parser.parse_args()

    print(f"🔧 壓縮模式: {args.quality}")
    print()

    gs_command = check_ghostscript()

    if not os.path.exists(INPUT_DIR):
        os.makedirs(INPUT_DIR)

        print(
            f"📁 已建立資料夾: {INPUT_DIR}"
        )
        print("請放入 PDF 後重新執行")

        return

    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)

    files = [
        f for f in os.listdir(INPUT_DIR)
        if f.lower().endswith(".pdf")
    ]

    if not files:
        print("📂 input 資料夾內沒有 PDF")
        return

    total_original = 0
    total_compressed = 0

    for file in files:
        try:
            original, compressed = process_file(
                gs_command,
                file,
                args.quality
            )

            total_original += original
            total_compressed += compressed

        except Exception as e:
            print(f"✖ 失敗: {file}")
            print(e)
            print()

    print("================================")

    if total_original > 0:
        reduction = total_original - total_compressed
        ratio = reduction / total_original * 100

        print(
            f"原始總大小: "
            f"{total_original / 1024 / 1024:.2f} MB"
        )

        print(
            f"壓縮總大小: "
            f"{total_compressed / 1024 / 1024:.2f} MB"
        )

        print(
            f"節省空間: "
            f"{reduction / 1024 / 1024:.2f} MB"
        )

        print(
            f"總壓縮率: "
            f"{ratio:.2f}%"
        )

    print("================================")
    os.system("PAUSE")


if __name__ == "__main__":
    main()

















沒有留言:

張貼留言

只要點兩下,就能將input資料夾內所有PDF進行容量壓縮-版本02

系列文章: 01. python 不管何時何地,只要點兩下,資料夾內的所有pdf都會合併成一個pdf https://skjhcreator.blogspot.com/2022/06/pythonpdfpdf.html 02. python 只要點兩下,分別對各資料夾內的pdf合...