Skip to main content

Python bindings for the pure-Rust office2pdf converter

Project description

office2pdf-python

Python bindings for office2pdf, a pure-Rust converter for DOCX, PPTX, and XLSX documents to PDF.

The package distribution is named office2pdf-python; the import package is office2pdf.

Install

python -m pip install office2pdf-python

Supported Python versions are 3.10 through 3.14. Wheels use PyO3 abi3-py310, so one wheel can support all compatible CPython versions for the same platform. CI targets Linux, macOS, and Windows.

Usage

from pathlib import Path

from office2pdf import ConvertOptions, Format, convert_bytes, convert_path

result = convert_path("report.docx")
Path("report.pdf").write_bytes(result.pdf)

options = ConvertOptions(paper_size="a4", landscape=False, include_warnings=True)
data = Path("slides.pptx").read_bytes()
result = convert_bytes(data, Format.PPTX, options)
Path("slides.pdf").write_bytes(result.pdf)

CLI

The package also installs an office2pdf command:

office2pdf input.docx output.pdf

The CLI accepts DOCX, PPTX, and XLSX input paths and writes the converted PDF bytes to the output path.

API

Format

Format identifies the input Office format for byte-based conversion:

  • Format.DOCX ("docx")
  • Format.PPTX ("pptx")
  • Format.XLSX ("xlsx")

PdfStandard

PdfStandard currently supports pdf/a-2b:

  • canonical: PdfStandard.PDF_A_2B
  • compatibility alias: PdfStandard.PDF_A_2_B

PdfStandard.from_value() accepts both forms and related normalizations ("pdf/a-2b", "pdfa2b").

PaperSize

  • PaperSize.A4
  • PaperSize.LETTER
  • PaperSize.LEGAL

CustomPaperSize

CustomPaperSize(width: float, height: float) stores explicit PDF point dimensions, matching upstream PaperSize::Custom (1 point = 1/72 inch).

ConvertOptions

All options are stored in a Python dataclass and translated to native options via to_native().

  • sheet_names: Sequence[str] | None

  • sheet_filter: Sequence[str] | None

    These are aliases for XLSX sheet selection. If both are provided, they must be equal.

  • slide_range: SlideRange | str | None

    SlideRange supports "1-5" parsing and also accepts explicit SlideRange(1, 5). Values are normalized to start-end strings for native conversion.

  • pdf_standard: PdfStandard | str | None

    Only pdf/a-2b is supported at this version.

  • paper_size: PaperSize | CustomPaperSize | str | None

    String values normalize to named page sizes.

  • font_paths: Sequence[str | pathlib.Path]

  • landscape: bool | None

  • tagged: bool | None

  • pdf_ua: bool | None

  • streaming: bool

  • streaming_chunk_size: int | None

  • include_warnings: bool

Unsupported options are rejected to preserve API compatibility with upstream office2pdf 0.6.0:

  • page_range: str | None
  • memory_limit_mb: int | None

ConversionResult

  • pdf: bytes
  • warnings: tuple[ConvertWarning, ...]
  • metrics: ConvertMetrics | None
  • warning_messages: tuple[str, ...] property collecting warning messages.

Warning types

Warning payloads from the native layer are mapped to typed subclasses of ConvertWarning:

  • UnsupportedElementWarning(format, element)
  • PartialElementWarning(format, element, detail)
  • FallbackUsedWarning(format, from_, to)
  • ParseSkippedWarning(format, reason)
  • and a base ConvertWarning for legacy/unknown forms.

ConvertMetrics

  • parse_duration
  • codegen_duration
  • compile_duration
  • total_duration
  • input_size_bytes
  • output_size_bytes
  • page_count

Duration fields are reported in seconds.

Functions

convert_bytes(data: bytes | bytearray | memoryview, format: Format | str, options: ConvertOptions | None = None) -> ConversionResult
convert_path(path: str | pathlib.Path, options: ConvertOptions | None = None) -> ConversionResult
infer_format(path: str | pathlib.Path) -> Format
  • infer_format() reads the file suffix and accepts only .docx, .pptx, or .xlsx.
  • convert_path() validates the file extension before conversion.
  • convert_bytes() requires an explicit input format.

Exceptions

Re-exported exception hierarchy:

  • Office2PdfError
  • UnsupportedFormatError
  • Office2PdfIoError
  • Office2PdfParseError
  • Office2PdfRenderError
  • UnsupportedEncryptionError
  • UnsupportedOptionError

API scope

Version 0.2.0 exposes the upstream office2pdf 0.6.0 conversion API: file/bytes conversion, conversion options, structured warnings, metrics, and typed errors.

The upstream pdf_ops feature (page_count, merge, split), internal IR/parser/render modules, TypeScript helpers, and WASM APIs are intentionally out of scope for this Python release.

Local development

Install Rust and Python 3.10 or newer. For local development, create and activate a virtual environment before running maturin develop:

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip maturin pytest
maturin develop --locked
python -m pytest
cargo fmt --check
cargo clippy --all-targets -- -D warnings

Build artifacts:

maturin build --locked --release --compatibility pypi
maturin sdist

No LibreOffice, Docker, Chromium, or external system service is required by this binding. Conversion behavior comes from the upstream pure-Rust crate.

CI

The CI workflow runs tests on ubuntu-latest, macos-latest, and windows-2022 for Python 3.10, 3.11, 3.12, 3.13, and 3.14. It builds and installs the extension as an editable package, runs pytest, and performs an import smoke test.

Release-style wheel building is checked once in a dedicated wheel smoke job. Full Linux, macOS, and Windows release wheels are built by the release workflow instead of every CI matrix job. Rust fmt and clippy run in a separate Ubuntu lint job.

Release

The release workflow runs on v* tags or manual dispatch. It builds Linux, macOS, and Windows wheels plus an sdist, then publishes with PyPI Trusted Publishing using GitHub OIDC (id-token: write). No PyPI API token is required.

Before the first publish, configure PyPI with a pending trusted publisher:

  • PyPI project name: office2pdf-python
  • Owner: agentsyaml
  • Repository: office2pdf-python
  • Workflow: release.yml
  • Environment: pypi

Create a matching GitHub environment named pypi and require manual approval for safer first releases. A pending trusted publisher can create the PyPI project on first use, but it does not reserve the project name before that first publish.

To publish a release automatically, update the version in pyproject.toml and Cargo.toml, commit the change, then push a matching tag:

git tag v0.2.0
git push origin v0.2.0

The tag push starts .github/workflows/release.yml, builds artifacts, publishes to PyPI after the pypi environment approval, and creates a GitHub Release for tag-triggered runs.

Upstream dependency

This package wraps office2pdf = "0.6" from crates.io. The upstream project is Apache-2.0 licensed and hosted at https://github.com/developer0hye/office2pdf.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

office2pdf_python-0.2.0.tar.gz (48.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

office2pdf_python-0.2.0-cp310-abi3-win_amd64.whl (25.7 MB view details)

Uploaded CPython 3.10+Windows x86-64

office2pdf_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

office2pdf_python-0.2.0-cp310-abi3-macosx_11_0_arm64.whl (26.8 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file office2pdf_python-0.2.0.tar.gz.

File metadata

  • Download URL: office2pdf_python-0.2.0.tar.gz
  • Upload date:
  • Size: 48.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for office2pdf_python-0.2.0.tar.gz
Algorithm Hash digest
SHA256 639da53cda8ae8ecfc8105d9f5b7cea6cf94ef0b3dbe026c231d46d9dff6c563
MD5 9bce8e3cf03c1cb1d3ce5f4e8702bf8a
BLAKE2b-256 6a7168e8992e852f3cc5de2d47238162302a2933c6b48f0e02318a594c81cc6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for office2pdf_python-0.2.0.tar.gz:

Publisher: release.yml on agentsyaml/office2pdf-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file office2pdf_python-0.2.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for office2pdf_python-0.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d1ba2a21068992d33bc012f347d4ff25a6c7f325c4186b789cf6b9271b54eb70
MD5 c39bdd6b35a5aab3fed6fbae23a4c88d
BLAKE2b-256 378cce8e958b0632ab5cbc60875b561cdae4e69e522dd91978eda38a31ed1104

See more details on using hashes here.

Provenance

The following attestation bundles were made for office2pdf_python-0.2.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on agentsyaml/office2pdf-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file office2pdf_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for office2pdf_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 97eec9bd20af06508d193c855637b2b538e99a4672c1e54ca654913ef883e1f3
MD5 203e7d048b8fc9674c91c9d1c2accd9e
BLAKE2b-256 17ab658fba1f0876738ff371fbb4c50439346a976c764e43e51537299ae58d9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for office2pdf_python-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on agentsyaml/office2pdf-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file office2pdf_python-0.2.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for office2pdf_python-0.2.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8b47b75f4c2a1819ce08a44468b05807df075e81bb7ce7f976415980f263dd7e
MD5 d6c0e55e12dce480dacd96a06dada35f
BLAKE2b-256 1a3e13b56709fce9b6763e4907e8cbb4deee413cc68fa3463c2e964867ac73af

See more details on using hashes here.

Provenance

The following attestation bundles were made for office2pdf_python-0.2.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on agentsyaml/office2pdf-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page