office2pdf-python 0.1.0__cp310-abi3-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- LICENSE +201 -0
- README.md +134 -0
- office2pdf/__init__.py +143 -0
- office2pdf/_native.pyd +0 -0
- office2pdf/_native.pyi +9 -0
- office2pdf/cli.py +31 -0
- office2pdf/py.typed +0 -0
- office2pdf_python-0.1.0.dist-info/METADATA +160 -0
- office2pdf_python-0.1.0.dist-info/RECORD +13 -0
- office2pdf_python-0.1.0.dist-info/WHEEL +4 -0
- office2pdf_python-0.1.0.dist-info/entry_points.txt +2 -0
- office2pdf_python-0.1.0.dist-info/licenses/LICENSE +201 -0
- office2pdf_python-0.1.0.dist-info/sboms/office2pdf-python.cyclonedx.json +15924 -0
LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# office2pdf-python
|
|
2
|
+
|
|
3
|
+
Python bindings for [`office2pdf`](https://github.com/developer0hye/office2pdf), a pure-Rust converter for DOCX, PPTX, and XLSX documents to PDF.
|
|
4
|
+
|
|
5
|
+
The package distribution is named `office2pdf-python`; the import package is `office2pdf`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
python -m pip install office2pdf-python
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
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.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
from office2pdf import ConvertOptions, Format, convert_bytes, convert_path
|
|
21
|
+
|
|
22
|
+
result = convert_path("report.docx")
|
|
23
|
+
Path("report.pdf").write_bytes(result.pdf)
|
|
24
|
+
|
|
25
|
+
options = ConvertOptions(paper_size="a4", landscape=False, include_warnings=True)
|
|
26
|
+
data = Path("slides.pptx").read_bytes()
|
|
27
|
+
result = convert_bytes(data, Format.PPTX, options)
|
|
28
|
+
Path("slides.pdf").write_bytes(result.pdf)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## CLI
|
|
32
|
+
|
|
33
|
+
The package also installs an `office2pdf` command:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
office2pdf input.docx output.pdf
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The CLI accepts DOCX, PPTX, and XLSX input paths and writes the converted PDF bytes to the output path.
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### `Format`
|
|
44
|
+
|
|
45
|
+
Enum values: `Format.DOCX`, `Format.PPTX`, and `Format.XLSX`.
|
|
46
|
+
|
|
47
|
+
### `ConvertOptions`
|
|
48
|
+
|
|
49
|
+
Dataclass fields:
|
|
50
|
+
|
|
51
|
+
- `page_range: str | None` — rejected when set because `office2pdf 0.6.0` does not expose a native field for it.
|
|
52
|
+
- `sheet_filter: Sequence[str] | None` — maps to upstream `sheet_names` for XLSX conversion.
|
|
53
|
+
- `slide_range: str | None` — accepts upstream strings like `"1-5"` or `"3"`.
|
|
54
|
+
- `paper_size: str | None` — accepts upstream `"a4"`, `"letter"`, or `"legal"`.
|
|
55
|
+
- `landscape: bool | None` — maps to upstream orientation control.
|
|
56
|
+
- `font_paths: Sequence[str | pathlib.Path]` — additional font directories.
|
|
57
|
+
- `pdf_standard: str | None` — currently supports `"pdf/a-2b"`.
|
|
58
|
+
- `include_warnings: bool` — controls whether returned warnings are included in `ConversionResult`.
|
|
59
|
+
- `memory_limit_mb: int | None` — rejected when set because `office2pdf 0.6.0` does not expose a native field for it.
|
|
60
|
+
- `streaming: bool` — enables upstream streaming mode for supported formats.
|
|
61
|
+
|
|
62
|
+
### `ConversionResult`
|
|
63
|
+
|
|
64
|
+
Dataclass fields:
|
|
65
|
+
|
|
66
|
+
- `pdf: bytes`
|
|
67
|
+
- `warnings: tuple[str, ...]`
|
|
68
|
+
- `metrics: Mapping[str, Any] | None`
|
|
69
|
+
|
|
70
|
+
### Functions
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
convert_bytes(data: bytes | bytearray | memoryview, format: Format | str, options: ConvertOptions | None = None) -> ConversionResult
|
|
74
|
+
convert_path(path: str | pathlib.Path, options: ConvertOptions | None = None) -> ConversionResult
|
|
75
|
+
infer_format(path: str | pathlib.Path) -> Format
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`convert_path()` validates the file extension before calling the native extension. `convert_bytes()` requires an explicit format.
|
|
79
|
+
|
|
80
|
+
## Local development
|
|
81
|
+
|
|
82
|
+
Install Rust and Python 3.10 or newer. For local development, create and activate a virtual environment before running `maturin develop`:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
python -m venv .venv
|
|
86
|
+
source .venv/bin/activate
|
|
87
|
+
python -m pip install --upgrade pip maturin pytest
|
|
88
|
+
maturin develop --locked
|
|
89
|
+
python -m pytest
|
|
90
|
+
cargo fmt --check
|
|
91
|
+
cargo clippy --all-targets -- -D warnings
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Build artifacts:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
maturin build --locked --release --compatibility pypi
|
|
98
|
+
maturin sdist
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
No LibreOffice, Docker, Chromium, or external system service is required by this binding. Conversion behavior comes from the upstream pure-Rust crate.
|
|
102
|
+
|
|
103
|
+
## CI
|
|
104
|
+
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
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.
|
|
108
|
+
|
|
109
|
+
## Release
|
|
110
|
+
|
|
111
|
+
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.
|
|
112
|
+
|
|
113
|
+
Before the first publish, configure PyPI with a pending trusted publisher:
|
|
114
|
+
|
|
115
|
+
- PyPI project name: `office2pdf-python`
|
|
116
|
+
- Owner: `agentsyaml`
|
|
117
|
+
- Repository: `office2pdf-python`
|
|
118
|
+
- Workflow: `release.yml`
|
|
119
|
+
- Environment: `pypi`
|
|
120
|
+
|
|
121
|
+
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.
|
|
122
|
+
|
|
123
|
+
To publish a release automatically, update the version in `pyproject.toml` and `Cargo.toml`, commit the change, then push a matching tag:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
git tag v0.1.0
|
|
127
|
+
git push origin v0.1.0
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
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.
|
|
131
|
+
|
|
132
|
+
## Upstream dependency
|
|
133
|
+
|
|
134
|
+
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>.
|
office2pdf/__init__.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from importlib import import_module, metadata
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Mapping, Sequence
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
__version__ = metadata.version("office2pdf-python")
|
|
11
|
+
except metadata.PackageNotFoundError:
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Format(Enum):
|
|
16
|
+
DOCX = "docx"
|
|
17
|
+
PPTX = "pptx"
|
|
18
|
+
XLSX = "xlsx"
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def from_value(cls, value: Format | str) -> Format:
|
|
22
|
+
if isinstance(value, cls):
|
|
23
|
+
return value
|
|
24
|
+
normalized = str(value).lower().lstrip(".")
|
|
25
|
+
for member in cls:
|
|
26
|
+
if member.value == normalized:
|
|
27
|
+
return member
|
|
28
|
+
raise ValueError("format must be one of: docx, pptx, xlsx")
|
|
29
|
+
|
|
30
|
+
def __str__(self) -> str:
|
|
31
|
+
return self.value
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class ConvertOptions:
|
|
36
|
+
page_range: str | None = None
|
|
37
|
+
sheet_filter: Sequence[str] | None = None
|
|
38
|
+
slide_range: str | None = None
|
|
39
|
+
paper_size: str | None = None
|
|
40
|
+
landscape: bool | None = None
|
|
41
|
+
font_paths: Sequence[str | Path] = field(default_factory=tuple)
|
|
42
|
+
pdf_standard: str | None = None
|
|
43
|
+
include_warnings: bool = True
|
|
44
|
+
memory_limit_mb: int | None = None
|
|
45
|
+
streaming: bool = False
|
|
46
|
+
|
|
47
|
+
def to_native(self) -> dict[str, Any]:
|
|
48
|
+
if self.page_range is not None:
|
|
49
|
+
raise ValueError("page_range is not supported by upstream office2pdf 0.6")
|
|
50
|
+
if self.memory_limit_mb is not None:
|
|
51
|
+
raise ValueError("memory_limit_mb is not supported by upstream office2pdf 0.6")
|
|
52
|
+
if self.sheet_filter is not None and not all(isinstance(item, str) for item in self.sheet_filter):
|
|
53
|
+
raise TypeError("sheet_filter must contain only strings")
|
|
54
|
+
if self.font_paths is not None and not all(isinstance(item, (str, Path)) for item in self.font_paths):
|
|
55
|
+
raise TypeError("font_paths must contain only str or pathlib.Path values")
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
"page_range": None,
|
|
59
|
+
"sheet_filter": list(self.sheet_filter) if self.sheet_filter is not None else None,
|
|
60
|
+
"slide_range": self.slide_range,
|
|
61
|
+
"paper_size": self.paper_size,
|
|
62
|
+
"landscape": self.landscape,
|
|
63
|
+
"font_paths": [str(path) for path in self.font_paths],
|
|
64
|
+
"pdf_standard": self.pdf_standard,
|
|
65
|
+
"include_warnings": self.include_warnings,
|
|
66
|
+
"memory_limit_mb": None,
|
|
67
|
+
"streaming": self.streaming,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True)
|
|
72
|
+
class ConversionResult:
|
|
73
|
+
pdf: bytes
|
|
74
|
+
warnings: tuple[str, ...] = ()
|
|
75
|
+
metrics: Mapping[str, Any] | None = None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def infer_format(path: str | Path) -> Format:
|
|
79
|
+
suffix = Path(path).suffix.lower().lstrip(".")
|
|
80
|
+
if not suffix:
|
|
81
|
+
raise ValueError("path has no extension; expected .docx, .pptx, or .xlsx")
|
|
82
|
+
return Format.from_value(suffix)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def convert_bytes(
|
|
86
|
+
data: bytes | bytearray | memoryview,
|
|
87
|
+
format: Format | str,
|
|
88
|
+
options: ConvertOptions | None = None,
|
|
89
|
+
) -> ConversionResult:
|
|
90
|
+
if not isinstance(data, (bytes, bytearray, memoryview)):
|
|
91
|
+
raise TypeError("data must be bytes-like")
|
|
92
|
+
payload = bytes(data)
|
|
93
|
+
if not payload:
|
|
94
|
+
raise ValueError("data must not be empty")
|
|
95
|
+
|
|
96
|
+
input_format = Format.from_value(format)
|
|
97
|
+
native = _native_module()
|
|
98
|
+
raw = native.convert_bytes(payload, input_format.value, _native_options(options))
|
|
99
|
+
return _result_from_native(raw)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def convert_path(path: str | Path, options: ConvertOptions | None = None) -> ConversionResult:
|
|
103
|
+
input_path = Path(path)
|
|
104
|
+
infer_format(input_path)
|
|
105
|
+
|
|
106
|
+
native = _native_module()
|
|
107
|
+
raw = native.convert_path(str(input_path), _native_options(options))
|
|
108
|
+
return _result_from_native(raw)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _native_options(options: ConvertOptions | None) -> dict[str, Any]:
|
|
112
|
+
return ConvertOptions().to_native() if options is None else options.to_native()
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _native_module() -> Any:
|
|
116
|
+
return import_module("office2pdf._native")
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _result_from_native(raw: Mapping[str, Any]) -> ConversionResult:
|
|
120
|
+
pdf = raw.get("pdf")
|
|
121
|
+
if not isinstance(pdf, bytes):
|
|
122
|
+
raise TypeError("native conversion result did not contain PDF bytes")
|
|
123
|
+
|
|
124
|
+
warnings = raw.get("warnings", ())
|
|
125
|
+
if not isinstance(warnings, Sequence) or isinstance(warnings, (str, bytes, bytearray)):
|
|
126
|
+
raise TypeError("native conversion result warnings must be a sequence of strings")
|
|
127
|
+
|
|
128
|
+
return ConversionResult(
|
|
129
|
+
pdf=pdf,
|
|
130
|
+
warnings=tuple(str(warning) for warning in warnings),
|
|
131
|
+
metrics=raw.get("metrics"),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
__all__ = [
|
|
136
|
+
"ConversionResult",
|
|
137
|
+
"ConvertOptions",
|
|
138
|
+
"Format",
|
|
139
|
+
"__version__",
|
|
140
|
+
"convert_bytes",
|
|
141
|
+
"convert_path",
|
|
142
|
+
"infer_format",
|
|
143
|
+
]
|
office2pdf/_native.pyd
ADDED
|
Binary file
|
office2pdf/_native.pyi
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Mapping
|
|
4
|
+
|
|
5
|
+
__version__: str
|
|
6
|
+
|
|
7
|
+
def convert_bytes(data: bytes, format: str, options: Mapping[str, Any] | None = None) -> dict[str, Any]: ...
|
|
8
|
+
|
|
9
|
+
def convert_path(path: str, options: Mapping[str, Any] | None = None) -> dict[str, Any]: ...
|
office2pdf/cli.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from office2pdf import convert_path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
12
|
+
parser = argparse.ArgumentParser(
|
|
13
|
+
prog="office2pdf",
|
|
14
|
+
description="Convert DOCX, PPTX, or XLSX files to PDF.",
|
|
15
|
+
)
|
|
16
|
+
parser.add_argument("input", type=Path, help="Input Office file (.docx, .pptx, or .xlsx)")
|
|
17
|
+
parser.add_argument("output", type=Path, help="Output PDF file")
|
|
18
|
+
args = parser.parse_args(argv)
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
result = convert_path(args.input)
|
|
22
|
+
args.output.write_bytes(result.pdf)
|
|
23
|
+
except Exception as exc:
|
|
24
|
+
print(f"office2pdf: {exc}", file=sys.stderr)
|
|
25
|
+
return 1
|
|
26
|
+
|
|
27
|
+
return 0
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if __name__ == "__main__":
|
|
31
|
+
raise SystemExit(main())
|
office2pdf/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: office2pdf-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Development Status :: 3 - Alpha
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Programming Language :: Rust
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Summary: Python bindings for the pure-Rust office2pdf converter
|
|
17
|
+
Keywords: office,pdf,docx,pptx,xlsx,pyo3,maturin
|
|
18
|
+
Author: office2pdf-python contributors
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
21
|
+
Project-URL: Homepage, https://github.com/agentsyaml/office2pdf-python
|
|
22
|
+
Project-URL: Issues, https://github.com/agentsyaml/office2pdf-python/issues
|
|
23
|
+
Project-URL: Repository, https://github.com/agentsyaml/office2pdf-python
|
|
24
|
+
Project-URL: Upstream, https://github.com/developer0hye/office2pdf
|
|
25
|
+
|
|
26
|
+
# office2pdf-python
|
|
27
|
+
|
|
28
|
+
Python bindings for [`office2pdf`](https://github.com/developer0hye/office2pdf), a pure-Rust converter for DOCX, PPTX, and XLSX documents to PDF.
|
|
29
|
+
|
|
30
|
+
The package distribution is named `office2pdf-python`; the import package is `office2pdf`.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
python -m pip install office2pdf-python
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
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.
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from pathlib import Path
|
|
44
|
+
|
|
45
|
+
from office2pdf import ConvertOptions, Format, convert_bytes, convert_path
|
|
46
|
+
|
|
47
|
+
result = convert_path("report.docx")
|
|
48
|
+
Path("report.pdf").write_bytes(result.pdf)
|
|
49
|
+
|
|
50
|
+
options = ConvertOptions(paper_size="a4", landscape=False, include_warnings=True)
|
|
51
|
+
data = Path("slides.pptx").read_bytes()
|
|
52
|
+
result = convert_bytes(data, Format.PPTX, options)
|
|
53
|
+
Path("slides.pdf").write_bytes(result.pdf)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## CLI
|
|
57
|
+
|
|
58
|
+
The package also installs an `office2pdf` command:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
office2pdf input.docx output.pdf
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The CLI accepts DOCX, PPTX, and XLSX input paths and writes the converted PDF bytes to the output path.
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
### `Format`
|
|
69
|
+
|
|
70
|
+
Enum values: `Format.DOCX`, `Format.PPTX`, and `Format.XLSX`.
|
|
71
|
+
|
|
72
|
+
### `ConvertOptions`
|
|
73
|
+
|
|
74
|
+
Dataclass fields:
|
|
75
|
+
|
|
76
|
+
- `page_range: str | None` — rejected when set because `office2pdf 0.6.0` does not expose a native field for it.
|
|
77
|
+
- `sheet_filter: Sequence[str] | None` — maps to upstream `sheet_names` for XLSX conversion.
|
|
78
|
+
- `slide_range: str | None` — accepts upstream strings like `"1-5"` or `"3"`.
|
|
79
|
+
- `paper_size: str | None` — accepts upstream `"a4"`, `"letter"`, or `"legal"`.
|
|
80
|
+
- `landscape: bool | None` — maps to upstream orientation control.
|
|
81
|
+
- `font_paths: Sequence[str | pathlib.Path]` — additional font directories.
|
|
82
|
+
- `pdf_standard: str | None` — currently supports `"pdf/a-2b"`.
|
|
83
|
+
- `include_warnings: bool` — controls whether returned warnings are included in `ConversionResult`.
|
|
84
|
+
- `memory_limit_mb: int | None` — rejected when set because `office2pdf 0.6.0` does not expose a native field for it.
|
|
85
|
+
- `streaming: bool` — enables upstream streaming mode for supported formats.
|
|
86
|
+
|
|
87
|
+
### `ConversionResult`
|
|
88
|
+
|
|
89
|
+
Dataclass fields:
|
|
90
|
+
|
|
91
|
+
- `pdf: bytes`
|
|
92
|
+
- `warnings: tuple[str, ...]`
|
|
93
|
+
- `metrics: Mapping[str, Any] | None`
|
|
94
|
+
|
|
95
|
+
### Functions
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
convert_bytes(data: bytes | bytearray | memoryview, format: Format | str, options: ConvertOptions | None = None) -> ConversionResult
|
|
99
|
+
convert_path(path: str | pathlib.Path, options: ConvertOptions | None = None) -> ConversionResult
|
|
100
|
+
infer_format(path: str | pathlib.Path) -> Format
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`convert_path()` validates the file extension before calling the native extension. `convert_bytes()` requires an explicit format.
|
|
104
|
+
|
|
105
|
+
## Local development
|
|
106
|
+
|
|
107
|
+
Install Rust and Python 3.10 or newer. For local development, create and activate a virtual environment before running `maturin develop`:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
python -m venv .venv
|
|
111
|
+
source .venv/bin/activate
|
|
112
|
+
python -m pip install --upgrade pip maturin pytest
|
|
113
|
+
maturin develop --locked
|
|
114
|
+
python -m pytest
|
|
115
|
+
cargo fmt --check
|
|
116
|
+
cargo clippy --all-targets -- -D warnings
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Build artifacts:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
maturin build --locked --release --compatibility pypi
|
|
123
|
+
maturin sdist
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
No LibreOffice, Docker, Chromium, or external system service is required by this binding. Conversion behavior comes from the upstream pure-Rust crate.
|
|
127
|
+
|
|
128
|
+
## CI
|
|
129
|
+
|
|
130
|
+
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.
|
|
131
|
+
|
|
132
|
+
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.
|
|
133
|
+
|
|
134
|
+
## Release
|
|
135
|
+
|
|
136
|
+
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.
|
|
137
|
+
|
|
138
|
+
Before the first publish, configure PyPI with a pending trusted publisher:
|
|
139
|
+
|
|
140
|
+
- PyPI project name: `office2pdf-python`
|
|
141
|
+
- Owner: `agentsyaml`
|
|
142
|
+
- Repository: `office2pdf-python`
|
|
143
|
+
- Workflow: `release.yml`
|
|
144
|
+
- Environment: `pypi`
|
|
145
|
+
|
|
146
|
+
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.
|
|
147
|
+
|
|
148
|
+
To publish a release automatically, update the version in `pyproject.toml` and `Cargo.toml`, commit the change, then push a matching tag:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
git tag v0.1.0
|
|
152
|
+
git push origin v0.1.0
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
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.
|
|
156
|
+
|
|
157
|
+
## Upstream dependency
|
|
158
|
+
|
|
159
|
+
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>.
|
|
160
|
+
|