office-translate 0.2.0__tar.gz
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.
- office_translate-0.2.0/LICENSE +21 -0
- office_translate-0.2.0/PKG-INFO +211 -0
- office_translate-0.2.0/README.md +170 -0
- office_translate-0.2.0/pyproject.toml +55 -0
- office_translate-0.2.0/setup.cfg +4 -0
- office_translate-0.2.0/src/office_translate/__init__.py +23 -0
- office_translate-0.2.0/src/office_translate/cli.py +108 -0
- office_translate-0.2.0/src/office_translate/config.py +125 -0
- office_translate-0.2.0/src/office_translate/core.py +91 -0
- office_translate-0.2.0/src/office_translate/errors.py +18 -0
- office_translate-0.2.0/src/office_translate/glossary.py +194 -0
- office_translate-0.2.0/src/office_translate/ooxml.py +349 -0
- office_translate-0.2.0/src/office_translate/py.typed +0 -0
- office_translate-0.2.0/src/office_translate/text.py +99 -0
- office_translate-0.2.0/src/office_translate/translator.py +237 -0
- office_translate-0.2.0/src/office_translate.egg-info/PKG-INFO +211 -0
- office_translate-0.2.0/src/office_translate.egg-info/SOURCES.txt +27 -0
- office_translate-0.2.0/src/office_translate.egg-info/dependency_links.txt +1 -0
- office_translate-0.2.0/src/office_translate.egg-info/entry_points.txt +2 -0
- office_translate-0.2.0/src/office_translate.egg-info/requires.txt +14 -0
- office_translate-0.2.0/src/office_translate.egg-info/top_level.txt +1 -0
- office_translate-0.2.0/tests/test_cli.py +110 -0
- office_translate-0.2.0/tests/test_config.py +44 -0
- office_translate-0.2.0/tests/test_docx.py +131 -0
- office_translate-0.2.0/tests/test_glossary.py +114 -0
- office_translate-0.2.0/tests/test_ooxml.py +104 -0
- office_translate-0.2.0/tests/test_pptx.py +82 -0
- office_translate-0.2.0/tests/test_text.py +88 -0
- office_translate-0.2.0/tests/test_translator.py +134 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hamid Mergan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: office-translate
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Translate DOCX, PPTX and Markdown files with any OpenAI-compatible model while keeping the formatting, with glossary support
|
|
5
|
+
Author: Hamid Mergan
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/hamidmergan/office-translate
|
|
8
|
+
Project-URL: Source, https://github.com/hamidmergan/office-translate
|
|
9
|
+
Project-URL: Issues, https://github.com/hamidmergan/office-translate/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/hamidmergan/office-translate/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: translation,docx,pptx,markdown,glossary,llm,ollama,openai
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Office/Business :: Office Suites
|
|
23
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: openai>=1.0
|
|
29
|
+
Requires-Dist: lxml>=4.9
|
|
30
|
+
Provides-Extra: test
|
|
31
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
32
|
+
Requires-Dist: python-docx>=1.0; extra == "test"
|
|
33
|
+
Requires-Dist: python-pptx>=0.6.23; extra == "test"
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
36
|
+
Requires-Dist: python-docx>=1.0; extra == "dev"
|
|
37
|
+
Requires-Dist: python-pptx>=0.6.23; extra == "dev"
|
|
38
|
+
Requires-Dist: build; extra == "dev"
|
|
39
|
+
Requires-Dist: twine; extra == "dev"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# office-translate
|
|
43
|
+
|
|
44
|
+
A lightweight Python utility for translating Office documents and text files with a model of your choice. Only the text is translated. Fonts, bold and italic, links, tables, layouts and images stay as they are. A glossary keeps fixed terms consistent.
|
|
45
|
+
|
|
46
|
+
office-translate currently supports:
|
|
47
|
+
|
|
48
|
+
- Word (DOCX, DOCM)
|
|
49
|
+
- PowerPoint (PPTX, PPTM)
|
|
50
|
+
- Markdown (MD)
|
|
51
|
+
- Plain text (TXT)
|
|
52
|
+
|
|
53
|
+
## Prerequisites
|
|
54
|
+
|
|
55
|
+
- Python 3.9 or newer
|
|
56
|
+
- A server with an OpenAI-compatible API and a text model (for example Ollama, vLLM, LM Studio, a university server or a cloud provider). A vision model is not needed.
|
|
57
|
+
- An API key is optional. Many self-hosted servers do not need one.
|
|
58
|
+
|
|
59
|
+
If you run your own model, your documents stay on your own network. Nothing leaves your machine or data center.
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install git+https://github.com/hamidmergan/office-translate.git
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Alternatively, you can install it from the source:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
git clone https://github.com/hamidmergan/office-translate.git
|
|
71
|
+
cd office-translate
|
|
72
|
+
pip install .
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Usage
|
|
76
|
+
|
|
77
|
+
### Command-Line
|
|
78
|
+
|
|
79
|
+
Translate a file into German:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
office-translate report.docx --to German --base-url http://localhost:11434/v1 --model your-model
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This writes `report.german.docx` next to `report.docx`. The language can be a name or a code (`--to de` gives `report.de.docx`). Several files work the same way:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
office-translate report.docx slides.pptx notes.md --to German --base-url http://localhost:11434/v1 --model your-model
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Use a glossary, set the source language or add instructions for the model:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
office-translate slides.pptx --to French --from English --glossary terms.csv \
|
|
95
|
+
--instructions "Formal tone, audience is engineers" \
|
|
96
|
+
--base-url http://localhost:11434/v1 --model your-model
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Use `-o` to choose the output. For one file it is a file, for several files it is a folder:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
office-translate report.docx -o out/bericht.docx --to German --base-url http://localhost:11434/v1 --model your-model
|
|
103
|
+
office-translate a.docx b.pptx -o out/ --to German --base-url http://localhost:11434/v1 --model your-model
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
If the target already exists, a number is added (`report.german-1.docx`). A file named explicitly with `-o` is overwritten. The input file is never changed. Errors appear on stderr. The exit code is 1 if at least one file failed.
|
|
107
|
+
|
|
108
|
+
### Python API
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from office_translate import translate_file
|
|
112
|
+
|
|
113
|
+
out = translate_file(
|
|
114
|
+
"report.docx",
|
|
115
|
+
target_lang="German",
|
|
116
|
+
glossary="terms.csv",
|
|
117
|
+
base_url="http://localhost:11434/v1",
|
|
118
|
+
model="your-model",
|
|
119
|
+
)
|
|
120
|
+
print(out) # report.german.docx
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
For many files, create a `Translator` once and reuse it:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from office_translate import Translator
|
|
127
|
+
|
|
128
|
+
translator = Translator("German", glossary={"invoice": "Rechnung"}, base_url=..., model=...)
|
|
129
|
+
for name in ["a.docx", "b.pptx"]:
|
|
130
|
+
translator.translate_file(name)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Plain strings work too:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from office_translate import translate_text
|
|
137
|
+
|
|
138
|
+
translate_text(["Good morning", "Thank you"], target_lang="Spanish", base_url=..., model=...)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Glossary
|
|
142
|
+
|
|
143
|
+
A glossary is a file with two columns, source term and target term:
|
|
144
|
+
|
|
145
|
+
```csv
|
|
146
|
+
source,target
|
|
147
|
+
invoice,Rechnung
|
|
148
|
+
machine learning,maschinelles Lernen
|
|
149
|
+
Acme Cloud,
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
- CSV with comma or semicolon, TSV, or JSON (`{"invoice": "Rechnung"}` or a list of `{"source": ..., "target": ...}`)
|
|
153
|
+
- A header row (`source,target` or `term,translation`) and lines starting with `#` are ignored
|
|
154
|
+
- An empty target keeps the term unchanged, which is useful for product names
|
|
155
|
+
- Matching ignores case and only matches whole words. Use `--case-sensitive` to match exact case
|
|
156
|
+
|
|
157
|
+
The model gets the glossary terms as required translations. It may inflect them and build compounds, so "maschinelles Lernen" can become "des maschinellen Lernens" and "Rechnung" can become "Rechnungsprozess". Terms with an empty target are replaced by placeholders, so the model cannot change them.
|
|
158
|
+
|
|
159
|
+
After translation, each paragraph is checked for its glossary terms. If a term is missing, the paragraph is translated once more with a reminder. If it is still missing, a warning is printed.
|
|
160
|
+
|
|
161
|
+
Use `--strict-glossary` (`strict_glossary=True` in Python) to replace all terms with placeholders. The terms then appear exactly as written, but they are not inflected.
|
|
162
|
+
|
|
163
|
+
### Settings
|
|
164
|
+
|
|
165
|
+
Order: argument, then environment variable, then default.
|
|
166
|
+
|
|
167
|
+
| Argument / flag | Environment variable | Default |
|
|
168
|
+
| --- | --- | --- |
|
|
169
|
+
| `base_url` / `--base-url` | `OFFICE_TRANSLATE_BASE_URL` | none, required |
|
|
170
|
+
| `model` / `--model` | `OFFICE_TRANSLATE_MODEL` | none, required |
|
|
171
|
+
| `api_key` / `--api-key` | `OFFICE_TRANSLATE_API_KEY` (then `OPENAI_API_KEY`) | optional |
|
|
172
|
+
| `max_file_mb` / `--max-file-mb` | `OFFICE_TRANSLATE_MAX_FILE_MB` | 100 |
|
|
173
|
+
| `batch_size` / `--batch-size` | `OFFICE_TRANSLATE_BATCH_SIZE` | 20 segments |
|
|
174
|
+
| `batch_chars` | `OFFICE_TRANSLATE_BATCH_CHARS` | 4000 characters |
|
|
175
|
+
| `concurrency` / `--concurrency` | `OFFICE_TRANSLATE_CONCURRENCY` | 4 |
|
|
176
|
+
| `timeout` / `--timeout` | `OFFICE_TRANSLATE_TIMEOUT` | 120 seconds |
|
|
177
|
+
| `max_retries` | `OFFICE_TRANSLATE_MAX_RETRIES` | 2 |
|
|
178
|
+
| `temperature` | `OFFICE_TRANSLATE_TEMPERATURE` | 0.1 |
|
|
179
|
+
|
|
180
|
+
Paragraphs are sent in batches. Repeated text is translated only once. If a reply is cut off or incomplete, the batch is split and the missing parts are sent again.
|
|
181
|
+
|
|
182
|
+
## What gets translated
|
|
183
|
+
|
|
184
|
+
- Word: body, tables, headers, footers, footnotes, endnotes, comments, text boxes and SmartArt
|
|
185
|
+
- PowerPoint: slides, tables, grouped shapes, speaker notes, chart labels, SmartArt and free text on layouts and masters (placeholder prompts stay as they are)
|
|
186
|
+
- Markdown: text, headings, lists and tables. Front matter, code blocks and indented code stay unchanged
|
|
187
|
+
|
|
188
|
+
Text that contains no letters, such as numbers or dates, is not sent.
|
|
189
|
+
|
|
190
|
+
## Limitations
|
|
191
|
+
|
|
192
|
+
- Quality depends on the chosen model. Check important results.
|
|
193
|
+
- Within a paragraph, parts with different formatting are marked for the model. If the model moves them around, the formatting may end up on different words. If the marks come back broken, each part is translated on its own.
|
|
194
|
+
- Field codes, equations and tracked deletions are not translated.
|
|
195
|
+
- Chart labels are updated in the slide, but the embedded spreadsheet behind the chart is not.
|
|
196
|
+
- Excel files are not supported.
|
|
197
|
+
- Password protected files are not supported.
|
|
198
|
+
- Markdown and text files must be UTF-8.
|
|
199
|
+
|
|
200
|
+
## Running Tests
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
pip install -e ".[test]"
|
|
204
|
+
pytest
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The tests run against a local fake server and need no real model.
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
MIT
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# office-translate
|
|
2
|
+
|
|
3
|
+
A lightweight Python utility for translating Office documents and text files with a model of your choice. Only the text is translated. Fonts, bold and italic, links, tables, layouts and images stay as they are. A glossary keeps fixed terms consistent.
|
|
4
|
+
|
|
5
|
+
office-translate currently supports:
|
|
6
|
+
|
|
7
|
+
- Word (DOCX, DOCM)
|
|
8
|
+
- PowerPoint (PPTX, PPTM)
|
|
9
|
+
- Markdown (MD)
|
|
10
|
+
- Plain text (TXT)
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
- Python 3.9 or newer
|
|
15
|
+
- A server with an OpenAI-compatible API and a text model (for example Ollama, vLLM, LM Studio, a university server or a cloud provider). A vision model is not needed.
|
|
16
|
+
- An API key is optional. Many self-hosted servers do not need one.
|
|
17
|
+
|
|
18
|
+
If you run your own model, your documents stay on your own network. Nothing leaves your machine or data center.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install git+https://github.com/hamidmergan/office-translate.git
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Alternatively, you can install it from the source:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
git clone https://github.com/hamidmergan/office-translate.git
|
|
30
|
+
cd office-translate
|
|
31
|
+
pip install .
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### Command-Line
|
|
37
|
+
|
|
38
|
+
Translate a file into German:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
office-translate report.docx --to German --base-url http://localhost:11434/v1 --model your-model
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
This writes `report.german.docx` next to `report.docx`. The language can be a name or a code (`--to de` gives `report.de.docx`). Several files work the same way:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
office-translate report.docx slides.pptx notes.md --to German --base-url http://localhost:11434/v1 --model your-model
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Use a glossary, set the source language or add instructions for the model:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
office-translate slides.pptx --to French --from English --glossary terms.csv \
|
|
54
|
+
--instructions "Formal tone, audience is engineers" \
|
|
55
|
+
--base-url http://localhost:11434/v1 --model your-model
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Use `-o` to choose the output. For one file it is a file, for several files it is a folder:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
office-translate report.docx -o out/bericht.docx --to German --base-url http://localhost:11434/v1 --model your-model
|
|
62
|
+
office-translate a.docx b.pptx -o out/ --to German --base-url http://localhost:11434/v1 --model your-model
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
If the target already exists, a number is added (`report.german-1.docx`). A file named explicitly with `-o` is overwritten. The input file is never changed. Errors appear on stderr. The exit code is 1 if at least one file failed.
|
|
66
|
+
|
|
67
|
+
### Python API
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from office_translate import translate_file
|
|
71
|
+
|
|
72
|
+
out = translate_file(
|
|
73
|
+
"report.docx",
|
|
74
|
+
target_lang="German",
|
|
75
|
+
glossary="terms.csv",
|
|
76
|
+
base_url="http://localhost:11434/v1",
|
|
77
|
+
model="your-model",
|
|
78
|
+
)
|
|
79
|
+
print(out) # report.german.docx
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For many files, create a `Translator` once and reuse it:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from office_translate import Translator
|
|
86
|
+
|
|
87
|
+
translator = Translator("German", glossary={"invoice": "Rechnung"}, base_url=..., model=...)
|
|
88
|
+
for name in ["a.docx", "b.pptx"]:
|
|
89
|
+
translator.translate_file(name)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Plain strings work too:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from office_translate import translate_text
|
|
96
|
+
|
|
97
|
+
translate_text(["Good morning", "Thank you"], target_lang="Spanish", base_url=..., model=...)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Glossary
|
|
101
|
+
|
|
102
|
+
A glossary is a file with two columns, source term and target term:
|
|
103
|
+
|
|
104
|
+
```csv
|
|
105
|
+
source,target
|
|
106
|
+
invoice,Rechnung
|
|
107
|
+
machine learning,maschinelles Lernen
|
|
108
|
+
Acme Cloud,
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
- CSV with comma or semicolon, TSV, or JSON (`{"invoice": "Rechnung"}` or a list of `{"source": ..., "target": ...}`)
|
|
112
|
+
- A header row (`source,target` or `term,translation`) and lines starting with `#` are ignored
|
|
113
|
+
- An empty target keeps the term unchanged, which is useful for product names
|
|
114
|
+
- Matching ignores case and only matches whole words. Use `--case-sensitive` to match exact case
|
|
115
|
+
|
|
116
|
+
The model gets the glossary terms as required translations. It may inflect them and build compounds, so "maschinelles Lernen" can become "des maschinellen Lernens" and "Rechnung" can become "Rechnungsprozess". Terms with an empty target are replaced by placeholders, so the model cannot change them.
|
|
117
|
+
|
|
118
|
+
After translation, each paragraph is checked for its glossary terms. If a term is missing, the paragraph is translated once more with a reminder. If it is still missing, a warning is printed.
|
|
119
|
+
|
|
120
|
+
Use `--strict-glossary` (`strict_glossary=True` in Python) to replace all terms with placeholders. The terms then appear exactly as written, but they are not inflected.
|
|
121
|
+
|
|
122
|
+
### Settings
|
|
123
|
+
|
|
124
|
+
Order: argument, then environment variable, then default.
|
|
125
|
+
|
|
126
|
+
| Argument / flag | Environment variable | Default |
|
|
127
|
+
| --- | --- | --- |
|
|
128
|
+
| `base_url` / `--base-url` | `OFFICE_TRANSLATE_BASE_URL` | none, required |
|
|
129
|
+
| `model` / `--model` | `OFFICE_TRANSLATE_MODEL` | none, required |
|
|
130
|
+
| `api_key` / `--api-key` | `OFFICE_TRANSLATE_API_KEY` (then `OPENAI_API_KEY`) | optional |
|
|
131
|
+
| `max_file_mb` / `--max-file-mb` | `OFFICE_TRANSLATE_MAX_FILE_MB` | 100 |
|
|
132
|
+
| `batch_size` / `--batch-size` | `OFFICE_TRANSLATE_BATCH_SIZE` | 20 segments |
|
|
133
|
+
| `batch_chars` | `OFFICE_TRANSLATE_BATCH_CHARS` | 4000 characters |
|
|
134
|
+
| `concurrency` / `--concurrency` | `OFFICE_TRANSLATE_CONCURRENCY` | 4 |
|
|
135
|
+
| `timeout` / `--timeout` | `OFFICE_TRANSLATE_TIMEOUT` | 120 seconds |
|
|
136
|
+
| `max_retries` | `OFFICE_TRANSLATE_MAX_RETRIES` | 2 |
|
|
137
|
+
| `temperature` | `OFFICE_TRANSLATE_TEMPERATURE` | 0.1 |
|
|
138
|
+
|
|
139
|
+
Paragraphs are sent in batches. Repeated text is translated only once. If a reply is cut off or incomplete, the batch is split and the missing parts are sent again.
|
|
140
|
+
|
|
141
|
+
## What gets translated
|
|
142
|
+
|
|
143
|
+
- Word: body, tables, headers, footers, footnotes, endnotes, comments, text boxes and SmartArt
|
|
144
|
+
- PowerPoint: slides, tables, grouped shapes, speaker notes, chart labels, SmartArt and free text on layouts and masters (placeholder prompts stay as they are)
|
|
145
|
+
- Markdown: text, headings, lists and tables. Front matter, code blocks and indented code stay unchanged
|
|
146
|
+
|
|
147
|
+
Text that contains no letters, such as numbers or dates, is not sent.
|
|
148
|
+
|
|
149
|
+
## Limitations
|
|
150
|
+
|
|
151
|
+
- Quality depends on the chosen model. Check important results.
|
|
152
|
+
- Within a paragraph, parts with different formatting are marked for the model. If the model moves them around, the formatting may end up on different words. If the marks come back broken, each part is translated on its own.
|
|
153
|
+
- Field codes, equations and tracked deletions are not translated.
|
|
154
|
+
- Chart labels are updated in the slide, but the embedded spreadsheet behind the chart is not.
|
|
155
|
+
- Excel files are not supported.
|
|
156
|
+
- Password protected files are not supported.
|
|
157
|
+
- Markdown and text files must be UTF-8.
|
|
158
|
+
|
|
159
|
+
## Running Tests
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
pip install -e ".[test]"
|
|
163
|
+
pytest
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The tests run against a local fake server and need no real model.
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "office-translate"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Translate DOCX, PPTX and Markdown files with any OpenAI-compatible model while keeping the formatting, with glossary support"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Hamid Mergan" }]
|
|
14
|
+
keywords = ["translation", "docx", "pptx", "markdown", "glossary", "llm", "ollama", "openai"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Intended Audience :: End Users/Desktop",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Office/Business :: Office Suites",
|
|
27
|
+
"Topic :: Text Processing :: Linguistic",
|
|
28
|
+
"Typing :: Typed",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"openai>=1.0",
|
|
32
|
+
"lxml>=4.9",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
test = ["pytest>=7", "python-docx>=1.0", "python-pptx>=0.6.23"]
|
|
37
|
+
dev = ["pytest>=7", "python-docx>=1.0", "python-pptx>=0.6.23", "build", "twine"]
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/hamidmergan/office-translate"
|
|
41
|
+
Source = "https://github.com/hamidmergan/office-translate"
|
|
42
|
+
Issues = "https://github.com/hamidmergan/office-translate/issues"
|
|
43
|
+
Changelog = "https://github.com/hamidmergan/office-translate/blob/main/CHANGELOG.md"
|
|
44
|
+
|
|
45
|
+
[project.scripts]
|
|
46
|
+
office-translate = "office_translate.cli:main"
|
|
47
|
+
|
|
48
|
+
[tool.setuptools.packages.find]
|
|
49
|
+
where = ["src"]
|
|
50
|
+
|
|
51
|
+
[tool.setuptools.package-data]
|
|
52
|
+
office_translate = ["py.typed"]
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Translate DOCX, PPTX, Markdown and text files while keeping the formatting."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.2.0"
|
|
4
|
+
|
|
5
|
+
from .core import SUPPORTED, translate_file, translate_text
|
|
6
|
+
from .errors import ConfigError, GlossaryError, ModelError, TranslateError, UnsupportedFileError
|
|
7
|
+
from .glossary import Glossary, load_glossary
|
|
8
|
+
from .translator import Translator
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"__version__",
|
|
12
|
+
"SUPPORTED",
|
|
13
|
+
"Translator",
|
|
14
|
+
"translate_file",
|
|
15
|
+
"translate_text",
|
|
16
|
+
"Glossary",
|
|
17
|
+
"load_glossary",
|
|
18
|
+
"TranslateError",
|
|
19
|
+
"ConfigError",
|
|
20
|
+
"GlossaryError",
|
|
21
|
+
"ModelError",
|
|
22
|
+
"UnsupportedFileError",
|
|
23
|
+
]
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import List, Optional
|
|
5
|
+
|
|
6
|
+
from . import __version__
|
|
7
|
+
from .core import output_path
|
|
8
|
+
from .errors import TranslateError
|
|
9
|
+
from .translator import Translator
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _parser() -> argparse.ArgumentParser:
|
|
13
|
+
p = argparse.ArgumentParser(
|
|
14
|
+
prog="office-translate",
|
|
15
|
+
description="Translate DOCX, PPTX, Markdown and text files with any OpenAI-compatible model.",
|
|
16
|
+
)
|
|
17
|
+
p.add_argument("files", nargs="+", help="Files to translate (.docx, .pptx, .md, .txt)")
|
|
18
|
+
p.add_argument("--to", required=True, dest="target", help="Target language, e.g. German or de")
|
|
19
|
+
p.add_argument("--from", dest="source", help="Source language. Default: detected by the model")
|
|
20
|
+
p.add_argument("--glossary", help="Glossary file (.csv, .tsv or .json) with source and target terms")
|
|
21
|
+
p.add_argument("--case-sensitive", action="store_true", help="Match glossary terms case-sensitively")
|
|
22
|
+
p.add_argument(
|
|
23
|
+
"--strict-glossary",
|
|
24
|
+
action="store_true",
|
|
25
|
+
help="Replace all glossary terms with placeholders. Exact terms, but no inflection",
|
|
26
|
+
)
|
|
27
|
+
p.add_argument("--instructions", help="Extra instructions for the model, e.g. tone or audience")
|
|
28
|
+
p.add_argument(
|
|
29
|
+
"-o",
|
|
30
|
+
"--output",
|
|
31
|
+
help="Output file (one input) or output folder (several inputs). "
|
|
32
|
+
"Default: <name>.<lang>.<ext> next to each input",
|
|
33
|
+
)
|
|
34
|
+
p.add_argument("--base-url", help="URL of the OpenAI-compatible server, e.g. http://localhost:11434/v1")
|
|
35
|
+
p.add_argument("--model", help="Name of the model")
|
|
36
|
+
p.add_argument("--api-key", help="API key, if the server requires one")
|
|
37
|
+
p.add_argument("--max-file-mb", type=float)
|
|
38
|
+
p.add_argument("--batch-size", type=int, help="Segments per request")
|
|
39
|
+
p.add_argument("--concurrency", type=int, help="Requests sent in parallel")
|
|
40
|
+
p.add_argument("--timeout", type=float, help="Seconds per request")
|
|
41
|
+
p.add_argument("--version", action="version", version=f"office-translate {__version__}")
|
|
42
|
+
return p
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _free(path: Path) -> Path:
|
|
46
|
+
"""Return path, or 'name-1.ext', 'name-2.ext', ... if it already exists."""
|
|
47
|
+
candidate, n = path, 1
|
|
48
|
+
while candidate.exists():
|
|
49
|
+
candidate = path.with_name(f"{path.stem}-{n}{path.suffix}")
|
|
50
|
+
n += 1
|
|
51
|
+
return candidate
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _target(src: Path, out: Optional[Path], many: bool, lang: str) -> Path:
|
|
55
|
+
generated = output_path(src, lang)
|
|
56
|
+
if out is None:
|
|
57
|
+
return _free(generated)
|
|
58
|
+
if many or out.is_dir():
|
|
59
|
+
return _free(out / generated.name)
|
|
60
|
+
# an explicitly named output file is overwritten
|
|
61
|
+
return out
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main(argv: Optional[List[str]] = None) -> int:
|
|
65
|
+
args = _parser().parse_args(argv)
|
|
66
|
+
many = len(args.files) > 1
|
|
67
|
+
out = Path(args.output) if args.output else None
|
|
68
|
+
|
|
69
|
+
if many and out is not None and out.exists() and not out.is_dir():
|
|
70
|
+
print("Error: with several files, -o must be a folder.", file=sys.stderr)
|
|
71
|
+
return 2
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
translator = Translator(
|
|
75
|
+
args.target,
|
|
76
|
+
args.source,
|
|
77
|
+
args.glossary,
|
|
78
|
+
args.instructions,
|
|
79
|
+
base_url=args.base_url,
|
|
80
|
+
model=args.model,
|
|
81
|
+
api_key=args.api_key,
|
|
82
|
+
case_sensitive=args.case_sensitive,
|
|
83
|
+
strict_glossary=args.strict_glossary,
|
|
84
|
+
max_file_mb=args.max_file_mb,
|
|
85
|
+
batch_size=args.batch_size,
|
|
86
|
+
concurrency=args.concurrency,
|
|
87
|
+
timeout=args.timeout,
|
|
88
|
+
)
|
|
89
|
+
except TranslateError as exc:
|
|
90
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
91
|
+
return 2
|
|
92
|
+
|
|
93
|
+
failed = 0
|
|
94
|
+
for name in args.files:
|
|
95
|
+
src = Path(name)
|
|
96
|
+
try:
|
|
97
|
+
target = _target(src, out, many, translator.target_lang)
|
|
98
|
+
written = translator.translate_file(src, target)
|
|
99
|
+
except (TranslateError, OSError) as exc:
|
|
100
|
+
failed += 1
|
|
101
|
+
print(f"Error: {name}: {exc}", file=sys.stderr)
|
|
102
|
+
continue
|
|
103
|
+
print(f"Wrote {written}", file=sys.stderr)
|
|
104
|
+
return 1 if failed else 0
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
if __name__ == "__main__":
|
|
108
|
+
sys.exit(main())
|