Skip to content

Python Integration zh

kudryavskiy_rv edited this page Jul 5, 2026 · 1 revision

Python 集成

零依赖的 office-print CLI 封装。

安装

将 office_print.py 复制到项目中 — 无需 pip,无需依赖。

快速开始

from office_print import convert, convert_bytes

# 文件
result = convert("report.docx", "report.pdf")
assert result.returncode == 0

# 字节
with open("template.docx", "rb") as f:
    data = f.read()
result = convert_bytes(data, ".docx", "output.pdf")

# 带选项
result = convert(
    "report.docx", "report.pdf",
    format="jpeg", jpeg_quality=85,
    paper="a4", landscape=True,
    font_paths=["./fonts"],
)

API

convert(input_file, output_file=None, **kwargs)

返回 subprocess.CompletedProcess。

参数 类型 默认值 说明
input_file str | Path — .docx、.xlsx、.pptx
output_file str | Path None 输出路径
output_dir str | Path None 批量输出目录
format str "pdf" "pdf"、"png"、"jpeg"
jpeg_quality int 92 1–100
paper str None "a4"、"letter"、"legal"
landscape bool False
font_paths list None 额外字体目录
sheets str None XLSX:"Sheet1,Data"
slides str None PPTX:"1-5"
streaming bool False 大型 XLSX
pdf_a bool False PDF/A-2b
tagged bool False 带标签的 PDF
pdf_ua bool False PDF/UA-1

convert_bytes(data, suffix, output_file=None, **kwargs)

与 convert() 类似但用于字节。使用临时文件。

merge_pdfs(inputs, output)

将多个 PDF 合并为一个。

split_pdf(input_pdf, output_dir, ranges)

按页面范围拆分 PDF。ranges = ["1-3", "5"]。

要求

  • office-print 在 PATH 中或与 office_print.py 同目录

Home-zh

Clone this wiki locally