This repository contains a collection of Python scripts for processing and compressing PDF and Word documents.
- Convert Word to PDF: Converts
.docxfiles to.pdf. - Image ompression: Compresses images within Word documents before converting to PDF.
- PDF Compression: Compresses existing PDF files using Ghostscript to reduce file size.
- PDF Labeling: Adds the filename as a label to the first page of a PDF.
本代码仓库包含一系列用于处理和压缩 PDF 及 Word 文档的 Python 脚本。
- Word 转 PDF: 将
.docx文件转换为.pdf文件。 - 图片压缩: 在将 Word 文档转换为 PDF 之前,预先压缩其中的图片以减小文件体积。
- PDF 压缩: 使用 Ghostscript 压缩已有的 PDF 文件以减小文件大小。
- PDF 标注: 将文件名作为标签添加到 PDF 的第一页。
- Python 3.x
- Ghostscript: Must be installed and accessible from the command line.
- Required Python packages can be installed using uv (recommended) or pip:
uv pip install docx2pdf pypdf reportlab pillow python-docx PyMuPDF # or pip install docx2pdf pypdf reportlab pillow python-docx PyMuPDF
- Python 3.x
- Ghostscript: 必须已安装并在命令行中可用。
- 所需的 Python 包可以使用 uv (推荐) 或 pip 安装:
uv pip install docx2pdf pypdf reportlab pillow python-docx PyMuPDF # 或 pip install docx2pdf pypdf reportlab pillow python-docx PyMuPDF
Each script is designed to be run from the command line and can be configured by editing the variables at the top of each file.
This script converts Word documents (.docx) to PDF. It first compresses images within the .docx file before converting it to reduce the final file size.
- Place your
.docxfiles in the主体结构directory (or change theINPUT_DIRvariable in the script). - Run the script:
python process_doc.py
- The converted PDF files will be saved in the
output_pdfsdirectory (or the configuredOUTPUT_DIR).
This script compresses PDF files using Ghostscript.
- Place the PDF files you want to compress into the
output_pdfsdirectory (or change theINPUT_DIRvariable). This is conveniently the output directory ofprocess_doc.py. - Run the script:
python process_pdf.py
- The compressed PDFs will be saved in the
compressed_pdfsdirectory (or the configuredOUTPUT_DIR).
| 参数 | 屏幕阅读 | 打印用途 |
|---|---|---|
target_dpi |
150 | 300 |
threshold |
1.0 | 1.5 |
image_quality |
75 | 85 |
This script adds a label with the filename to the top-left corner of the first page of a PDF.
- Place the PDF files you want to label in the
主体结构压缩directory (or changeINPUT_DIR). - Run the script:
python label_pdf.py
- The labeled PDFs will be saved in the
labeled_pdfsdirectory (or the configuredOUTPUT_DIR).
每个脚本都可以从命令行运行,并通过编辑文件顶部的变量进行配置。
该脚本将 Word 文档(.docx)转换为 PDF。它会先压缩 .docx 文件中的图片,然后再进行转换,以减小最终文件大小。
- 将您的
.docx文件放入主体结构目录(或在脚本中更改INPUT_DIR变量)。 - 运行脚本:
python process_doc.py
- 转换后的 PDF 文件将保存在
output_pdfs目录(或配置的OUTPUT_DIR)中。
该脚本使用 Ghostscript 压缩 PDF 文件。
- 将需要压缩的 PDF 文件放入
output_pdfs目录(或更改INPUT_DIR变量)。该目录恰好是process_doc.py的输出目录。 - 运行脚本:
python process_pdf.py
- 压缩后的 PDF 将保存在
compressed_pdfs目录(或配置的OUTPUT_DIR)中。
该脚本在 PDF 的第一页左上角添加一个带有文件名的标签。
- 将需要标注的 PDF 文件放入
主体结构压缩目录(或更改INPUT_DIR)。 - 运行脚本:
python label_pdf.py
- 标注后的 PDF 将保存在
labeled_pdfs目录(或配置的OUTPUT_DIR)中。
It is recommended to use uv for dependency management:
# Install dependencies and create virtual environment
uv sync
# Run the application
uv run main.pyOr using standard pip:
pip install -r requirements.txt
python main.pyThe application can be packaged into a standalone executable for different platforms using PyInstaller.
Install the development dependencies:
uv pip install pyinstaller
# or
pip install pyinstallerTo build for your current platform, run:
cd packaging
uv run build.py
# or
python build.pyThe resulting executable or app bundle will be located in packaging/dist/.
Multi-platform builds are handled automatically via GitHub Actions whenever a new tag starting with v is pushed.
This project is licensed under the terms of the LICENSE file.