ansibangla 1.0.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.
- ansibangla-1.0.0/LICENSE +21 -0
- ansibangla-1.0.0/PKG-INFO +283 -0
- ansibangla-1.0.0/README.md +255 -0
- ansibangla-1.0.0/ansibangla/__init__.py +13 -0
- ansibangla-1.0.0/ansibangla/__main__.py +3 -0
- ansibangla-1.0.0/ansibangla/_common.py +230 -0
- ansibangla-1.0.0/ansibangla/api.py +173 -0
- ansibangla-1.0.0/ansibangla/bijoy.py +336 -0
- ansibangla-1.0.0/ansibangla/blocks.py +104 -0
- ansibangla-1.0.0/ansibangla/cli.py +54 -0
- ansibangla-1.0.0/ansibangla/docxexport.py +454 -0
- ansibangla-1.0.0/ansibangla/docxinput.py +529 -0
- ansibangla-1.0.0/ansibangla/docxrender.py +539 -0
- ansibangla-1.0.0/ansibangla/epubexport.py +396 -0
- ansibangla-1.0.0/ansibangla/fonts/NotoSerifBengaliW94-Bold.ttf +0 -0
- ansibangla-1.0.0/ansibangla/fonts/NotoSerifBengaliW94-Regular.ttf +0 -0
- ansibangla-1.0.0/ansibangla/fonts/OFL.txt +93 -0
- ansibangla-1.0.0/ansibangla/mdexport.py +71 -0
- ansibangla-1.0.0/ansibangla/pdfbuild.py +267 -0
- ansibangla-1.0.0/ansibangla.egg-info/PKG-INFO +283 -0
- ansibangla-1.0.0/ansibangla.egg-info/SOURCES.txt +26 -0
- ansibangla-1.0.0/ansibangla.egg-info/dependency_links.txt +1 -0
- ansibangla-1.0.0/ansibangla.egg-info/entry_points.txt +2 -0
- ansibangla-1.0.0/ansibangla.egg-info/requires.txt +1 -0
- ansibangla-1.0.0/ansibangla.egg-info/top_level.txt +1 -0
- ansibangla-1.0.0/pyproject.toml +43 -0
- ansibangla-1.0.0/setup.cfg +4 -0
- ansibangla-1.0.0/tests/test_convert.py +293 -0
ansibangla-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ragib Ahsan
|
|
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,283 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ansibangla
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Convert ANSI Bangla (Bijoy / SutonnyMJ) PDFs and Word documents to Unicode, keeping their formatting: PDF, Word, EPUB, Markdown and printable HTML.
|
|
5
|
+
Author: Ragib Ahsan
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://ansibangla.vercel.app/
|
|
8
|
+
Keywords: bengali,bangla,ansi,sutonnymj,bijoy,unicode,pdf,docx,epub,converter
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
12
|
+
Classifier: Natural Language :: Bengali
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Text Processing :: Fonts
|
|
22
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: pymupdf<2,>=1.24.3
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# ansibangla
|
|
30
|
+
|
|
31
|
+
Convert ANSI Bangla (Bijoy / SutonnyMJ) PDFs and Word documents to Unicode, keeping their
|
|
32
|
+
formatting.
|
|
33
|
+
|
|
34
|
+
Many Bengali books and documents were typed with Bijoy in fonts such as SutonnyMJ. This
|
|
35
|
+
"ANSI Bangla" is not Unicode: the file stores Latin key codes, so copied text comes out as
|
|
36
|
+
gibberish like `wbwØ©avq` instead of `নির্দ্বিধায়`. This tool reads such a PDF or `.docx`
|
|
37
|
+
and writes real Unicode Bengali as PDF, Word, EPUB or Markdown, with the layout kept.
|
|
38
|
+
|
|
39
|
+
*Bijoy and SutonnyMJ are names of their owners; this project is not affiliated with them.*
|
|
40
|
+
|
|
41
|
+
You choose the outputs with `-f`/`--formats`: any combination of `pdf`, `docx`, `epub`,
|
|
42
|
+
`md` and `html`. Without it you get the same format as the input: a PDF for a PDF, a `.docx` for
|
|
43
|
+
a `.docx`.
|
|
44
|
+
|
|
45
|
+
**Word input.** The `.docx` output is the same document with its SutonnyMJ text in
|
|
46
|
+
Unicode. Styles, paragraph spacing, alignment, page setup, tables, pictures, headers and
|
|
47
|
+
footers are left as they are; text is set at `--scale` of its size, with exact line
|
|
48
|
+
spacing equal to what SutonnyMJ gave, so lines and pages stay where they were. PDF output
|
|
49
|
+
lays that document out at its page size and margins (printed with Chrome; headers,
|
|
50
|
+
footers and footnotes are not drawn). EPUB output starts a chapter at each large bold
|
|
51
|
+
heading, with bold headings at body size as sections.
|
|
52
|
+
|
|
53
|
+
The outputs, for PDF or Word input:
|
|
54
|
+
|
|
55
|
+
| File | When | What it is |
|
|
56
|
+
|------|------|------------|
|
|
57
|
+
| `<name>_unicode.md` | `md` | Markdown following the layout: paragraphs, headings, bold and italic runs, centred lines and pictures (saved in `<name>_unicode_images/`). |
|
|
58
|
+
| `<name>_unicode.pdf` | `pdf` | Same page layout in a Unicode Bengali font (Noto Serif Bengali), with pictures and page colour kept. Text is searchable and copyable. |
|
|
59
|
+
| `<name>_unicode.docx` | `docx` | Word document with the source's formatting: page size, margins, page colour, line spacing, justified paragraphs with first-line indents, centred headings, framed pictures, bold and italic, page numbers. One page per source page. Fonts are embedded. |
|
|
60
|
+
| `<name>_unicode.epub` | `epub` | Reflowable EPUB 3 e-book: chapters split at headings, table of contents, justified paragraphs with indents, bold and italic, pictures, and the printed page numbers as a page list. Fonts are embedded. |
|
|
61
|
+
| `<name>_unicode.html` | `html` | The PDF's pages as one self-contained HTML file (fonts and pictures embedded). Open it in a browser and print to PDF; no Chrome install is needed. Print it once the page has finished loading, at 100% scale with no margins or headers. |
|
|
62
|
+
|
|
63
|
+
## Requirements
|
|
64
|
+
|
|
65
|
+
- Python 3.9 or newer
|
|
66
|
+
- [PyMuPDF](https://pymupdf.readthedocs.io/) (installed automatically)
|
|
67
|
+
- Google Chrome or Chromium, **only for PDF output**. Markdown, DOCX, EPUB and HTML output need no browser.
|
|
68
|
+
|
|
69
|
+
## Install
|
|
70
|
+
|
|
71
|
+
From PyPI, into a virtual environment:
|
|
72
|
+
|
|
73
|
+
```sh
|
|
74
|
+
python3 -m venv .venv
|
|
75
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
76
|
+
pip install ansibangla
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Or as a stand-alone command with [pipx](https://pipx.pypa.io/) or [uv](https://docs.astral.sh/uv/):
|
|
80
|
+
`pipx install ansibangla` or `uv tool install ansibangla`.
|
|
81
|
+
|
|
82
|
+
No install at all: the web app at <https://ansibangla.vercel.app/> runs the same converter
|
|
83
|
+
in your browser.
|
|
84
|
+
|
|
85
|
+
For development, from the `python/` folder of a checkout (code changes take effect
|
|
86
|
+
without reinstalling):
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
pip install -e . pytest
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Chrome / Chromium
|
|
93
|
+
|
|
94
|
+
The PDF output is laid out as HTML and printed with headless Chrome. The tool looks for,
|
|
95
|
+
in order:
|
|
96
|
+
|
|
97
|
+
1. the `--chrome` option (or `chrome=` argument in Python),
|
|
98
|
+
2. the `ANSIBANGLA_CHROME` environment variable,
|
|
99
|
+
3. `google-chrome`, `google-chrome-stable`, `chromium`, `chromium-browser` or `chrome` on your `PATH`.
|
|
100
|
+
|
|
101
|
+
On macOS and Windows Chrome is usually not on `PATH`, so point to it:
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
# macOS
|
|
105
|
+
export ANSIBANGLA_CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
|
|
106
|
+
|
|
107
|
+
# Windows (PowerShell)
|
|
108
|
+
$env:ANSIBANGLA_CHROME = "C:\Program Files\Google\Chrome\Application\chrome.exe"
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
On Debian/Ubuntu, `sudo apt install chromium` (or install Google Chrome) is enough.
|
|
112
|
+
|
|
113
|
+
## Command line usage
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
ansibangla book.pdf # Unicode PDF into the current folder
|
|
117
|
+
ansibangla book.pdf -o out/ # into out/
|
|
118
|
+
ansibangla book.pdf -f docx # Word only (no Chrome needed)
|
|
119
|
+
ansibangla book.pdf -f pdf docx epub md # all four
|
|
120
|
+
ansibangla book.pdf -f pdf,docx # commas work too, as does -f pdf -f docx
|
|
121
|
+
ansibangla book.pdf -f epub --title 'বইয়ের নাম' # e-book with a title
|
|
122
|
+
ansibangla a.pdf b.pdf -f md # several files, Markdown only
|
|
123
|
+
ansibangla story.docx -o out/ # Word input: Unicode .docx
|
|
124
|
+
ansibangla story.docx -f pdf epub # Word input: PDF + e-book
|
|
125
|
+
ansibangla book.pdf --scale 0.85
|
|
126
|
+
ansibangla book.pdf --chrome /path/to/chrome
|
|
127
|
+
python -m ansibangla book.pdf # same, without the installed script
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Options:
|
|
131
|
+
|
|
132
|
+
| Option | Default | Meaning |
|
|
133
|
+
|--------|---------|---------|
|
|
134
|
+
| `-o`, `--out-dir` | `.` | Output folder (created if missing). |
|
|
135
|
+
| `-f`, `--formats` | the input's format | Outputs: any of `pdf`, `docx`, `epub`, `md`, `html`, separated by spaces or commas, or by repeating `-f`. Without it: `pdf` for PDFs, `docx` for `.docx` files. |
|
|
136
|
+
| `--scale` | `0.9` | Font-size factor for the Unicode PDF and DOCX (also for `.docx` input). Lower it if text looks crowded. |
|
|
137
|
+
| `--chrome` | auto | Path to Chrome or Chromium. |
|
|
138
|
+
| `--title` | first heading | Title of the EPUB. |
|
|
139
|
+
| `--keep-latin` | none | File listing English words typed in the SutonnyMJ font that stay as typed (one per line or space-separated; `#` starts a comment). Bijoy reads Latin letters as Bengali, so such words otherwise come out as Bengali gibberish. |
|
|
140
|
+
|
|
141
|
+
The command prints each written file's path. If one input fails, it reports the error,
|
|
142
|
+
carries on with the rest, and exits with status 1.
|
|
143
|
+
|
|
144
|
+
## Python usage
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from ansibangla import convert_file, convert_pdf, convert_text
|
|
148
|
+
|
|
149
|
+
paths = convert_pdf('book.pdf', 'out/')
|
|
150
|
+
# {'pdf': 'out/book_unicode.pdf'}
|
|
151
|
+
|
|
152
|
+
convert_pdf('book.pdf', 'out/', formats=('docx', 'md'), stem='chapter1')
|
|
153
|
+
# {'docx': 'out/chapter1_unicode.docx', 'md': 'out/chapter1_unicode.md'}
|
|
154
|
+
|
|
155
|
+
convert_file('story.docx', 'out/', formats=('pdf', 'epub')) # picks by extension
|
|
156
|
+
|
|
157
|
+
convert_text('wbwØ©avq') # 'নির্দ্বিধায়'
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`convert_pdf(src, out_dir='.', *, stem=None, formats=('pdf',), scale=0.9, font_dir=None, chrome=None, title=None)`
|
|
161
|
+
|
|
162
|
+
- `stem`: base name for the output files (default: input file name).
|
|
163
|
+
- `formats`: any combination of `'pdf'`, `'docx'`, `'epub'`, `'md'`, `'html'`.
|
|
164
|
+
- `scale`: font-size factor for the PDF and DOCX.
|
|
165
|
+
- `font_dir`: folder with replacement fonts (default: the bundled Noto Serif Bengali).
|
|
166
|
+
- `chrome`: path to Chrome or Chromium.
|
|
167
|
+
- `title`: EPUB title (default: the first heading).
|
|
168
|
+
- `keep_latin`: English words typed in the SutonnyMJ font that stay as typed.
|
|
169
|
+
|
|
170
|
+
`convert_docx(src, out_dir='.', *, stem=None, formats=('docx',), font_dir=None, scale=0.9, chrome=None, title=None)`
|
|
171
|
+
converts a Word document; `convert_file(src, out_dir, ...)` picks `convert_pdf` or
|
|
172
|
+
`convert_docx` by the file extension.
|
|
173
|
+
|
|
174
|
+
`convert_text(text)` converts a single SutonnyMJ string, as extracted from a PDF, to
|
|
175
|
+
Unicode. Useful for text you copied out of a Bijoy-typed document.
|
|
176
|
+
|
|
177
|
+
## How it works
|
|
178
|
+
|
|
179
|
+
- **Character mapping.** SutonnyMJ key codes are mapped to Unicode, pre-base vowel signs
|
|
180
|
+
(ি ে ৈ) and reph (র্) are moved into logical order, and glyphs that PDF extraction
|
|
181
|
+
mis-codes are fixed. English words typed in the same font are kept as typed when listed
|
|
182
|
+
with `--keep-latin`.
|
|
183
|
+
- **Layout measurement.** Body font size, left and right text edges, the page-number zone
|
|
184
|
+
and pictures are measured from the PDF itself. Nothing is hard-coded for one book.
|
|
185
|
+
- **PDF rebuild.** Body text is rebuilt as justified paragraphs. Each block is fitted in
|
|
186
|
+
Chrome to the same number of lines as its source, so text does not collide with
|
|
187
|
+
headings or English lines. Page breaks are kept.
|
|
188
|
+
- **DOCX rebuild.** The document is written directly as WordprocessingML, with no extra
|
|
189
|
+
package. Each source page starts a new page, and blocks keep their vertical positions
|
|
190
|
+
through measured spacing. A paragraph that runs on to the next page is split there.
|
|
191
|
+
The bundled fonts are embedded, so Word and LibreOffice show them even where they are
|
|
192
|
+
not installed.
|
|
193
|
+
- **EPUB build.** The e-book is reflowable: the reader's device breaks lines and pages,
|
|
194
|
+
so there is no line-breaking mismatch to manage. It keeps the structure: a chapter
|
|
195
|
+
starts at each large heading, with the centred line above it (such as "অধ্যায় ১") as its
|
|
196
|
+
opening. Bold subheadings at the left margin become section headings in the table of
|
|
197
|
+
contents. Extra vertical space in the source, such as the blank line above a subheading,
|
|
198
|
+
is measured from the line positions and kept. Each source page gets an invisible page marker, so readers that support page
|
|
199
|
+
lists can go to a printed page number. It is written directly with `zipfile` and
|
|
200
|
+
passes EPUBCheck.
|
|
201
|
+
- **Word input.** Word splits text into many runs (spell-check marks alone cut it every
|
|
202
|
+
few words). Runs with the same formatting are merged, and a word still split across
|
|
203
|
+
differently formatted runs is moved to one side, so the reordering sees whole words.
|
|
204
|
+
Only SutonnyMJ runs change: they get the Unicode font, and Word's complex-script size,
|
|
205
|
+
bold and italic (which Bengali Unicode is laid out with) are set to match. Zero-width
|
|
206
|
+
joiners typed into the Bijoy text are dropped, since they change how Unicode conjuncts
|
|
207
|
+
are drawn.
|
|
208
|
+
|
|
209
|
+
## Project layout
|
|
210
|
+
|
|
211
|
+
```
|
|
212
|
+
ansibangla/
|
|
213
|
+
bijoy.py SutonnyMJ -> Unicode mapping (MAP, PDF_FIXUPS) and reordering
|
|
214
|
+
_common.py reading PDF spans, lines, pictures and measuring page layout
|
|
215
|
+
blocks.py reading order as blocks (paragraphs, headings, pictures) for MD, DOCX, EPUB
|
|
216
|
+
mdexport.py Markdown output
|
|
217
|
+
docxexport.py Word (.docx) output
|
|
218
|
+
epubexport.py EPUB 3 e-book output
|
|
219
|
+
pdfbuild.py HTML layout -> printable HTML, or headless Chrome -> PDF output
|
|
220
|
+
docxinput.py Word (.docx) input: SutonnyMJ runs converted in place
|
|
221
|
+
docxrender.py PDF, HTML and EPUB from a converted Word document
|
|
222
|
+
api.py convert_file / convert_pdf / convert_docx / convert_text
|
|
223
|
+
cli.py command line
|
|
224
|
+
fonts/ Noto Serif Bengali W94 (OFL), sized to match SutonnyMJ
|
|
225
|
+
tests/ pytest suite
|
|
226
|
+
tools/
|
|
227
|
+
make_fonts.py rebuilds the bundled fonts from the variable Noto Serif Bengali
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Limits
|
|
231
|
+
|
|
232
|
+
- Line breaks in body text differ from the source, because Unicode Bengali is wider.
|
|
233
|
+
Page breaks are kept.
|
|
234
|
+
- Glyph mappings were checked against real SutonnyMJ books, not against the publisher's font.
|
|
235
|
+
**Proofread the output.**
|
|
236
|
+
- Only text set in a font whose name contains `MJ` is converted; other fonts pass through unchanged.
|
|
237
|
+
- Scanned PDFs (images of pages, no text layer) are not supported; use OCR for those.
|
|
238
|
+
- The Markdown has no justification or page numbers; it is for reading and editing.
|
|
239
|
+
- In the DOCX, text flows within each page, so line breaks differ from the source and a
|
|
240
|
+
page may end a line or two early. The last line before a paragraph splits across pages
|
|
241
|
+
is not justified. Page numbers use Latin digits (Word has no Bengali page-number format).
|
|
242
|
+
If a page overflows on to an extra page, lower `--scale`.
|
|
243
|
+
- The EPUB does not keep the page colour or exact positions, because e-readers apply their
|
|
244
|
+
own theme and layout. Readers that let you choose a font replace the embedded one.
|
|
245
|
+
|
|
246
|
+
### Fixing a wrong character
|
|
247
|
+
|
|
248
|
+
If some word comes out wrong, the fix is usually one entry in `ansibangla/bijoy.py`:
|
|
249
|
+
|
|
250
|
+
- `PDF_FIXUPS`: characters the PDF extractor reports wrongly, replaced before mapping.
|
|
251
|
+
- `MAP`: SutonnyMJ sequences and their Unicode output. Longer sequences must come first.
|
|
252
|
+
|
|
253
|
+
Find the raw text with PyMuPDF (`page.get_text('rawdict')`), add the entry, and add a
|
|
254
|
+
case to `tests/test_convert.py`.
|
|
255
|
+
|
|
256
|
+
## Tests
|
|
257
|
+
|
|
258
|
+
```sh
|
|
259
|
+
pip install -e . pytest
|
|
260
|
+
pytest
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
The sample-book tests run only when PDF or `.docx` files are present in `sample/` at the
|
|
264
|
+
top of the repository. That folder is ignored by git, because sample books are usually
|
|
265
|
+
copyrighted; put your own test files there.
|
|
266
|
+
|
|
267
|
+
## Fonts and licence
|
|
268
|
+
|
|
269
|
+
`ansibangla/fonts/` holds Noto Serif Bengali W94 (Regular and Bold), static instances of
|
|
270
|
+
Noto Serif Bengali, © The Noto Project Authors, licensed under the SIL Open Font
|
|
271
|
+
License 1.1. See `ansibangla/fonts/OFL.txt`.
|
|
272
|
+
|
|
273
|
+
They are cut to match SutonnyMJ's proportions. SutonnyMJ draws its letters about 11%
|
|
274
|
+
smaller than Unicode Bengali fonts at the same point size, so text is set at 0.9 of the
|
|
275
|
+
source size (`--scale`). At that size, Noto Serif Bengali's width axis at 94 gives the
|
|
276
|
+
same line widths as SutonnyMJ: 0.997 of the source width, measured on SutonnyMJ books.
|
|
277
|
+
For comparison, the regular width gives 1.036 and SemiCondensed gives 0.957. The
|
|
278
|
+
instances have their own family name, so an installed Noto Serif Bengali of another
|
|
279
|
+
width is never used in their place. One placement rule is also changed. With া, ো or
|
|
280
|
+
ৌ, Noto puts chandrabindu far left (দাঁ) or past the syllable (পৌঁ). The bundled fonts
|
|
281
|
+
centre it over the join between the letter and its vowel stroke, as SutonnyMJ does.
|
|
282
|
+
Everywhere else Noto's placement is kept. To rebuild them: `pip install fonttools && python
|
|
283
|
+
tools/make_fonts.py`.
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# ansibangla
|
|
2
|
+
|
|
3
|
+
Convert ANSI Bangla (Bijoy / SutonnyMJ) PDFs and Word documents to Unicode, keeping their
|
|
4
|
+
formatting.
|
|
5
|
+
|
|
6
|
+
Many Bengali books and documents were typed with Bijoy in fonts such as SutonnyMJ. This
|
|
7
|
+
"ANSI Bangla" is not Unicode: the file stores Latin key codes, so copied text comes out as
|
|
8
|
+
gibberish like `wbwØ©avq` instead of `নির্দ্বিধায়`. This tool reads such a PDF or `.docx`
|
|
9
|
+
and writes real Unicode Bengali as PDF, Word, EPUB or Markdown, with the layout kept.
|
|
10
|
+
|
|
11
|
+
*Bijoy and SutonnyMJ are names of their owners; this project is not affiliated with them.*
|
|
12
|
+
|
|
13
|
+
You choose the outputs with `-f`/`--formats`: any combination of `pdf`, `docx`, `epub`,
|
|
14
|
+
`md` and `html`. Without it you get the same format as the input: a PDF for a PDF, a `.docx` for
|
|
15
|
+
a `.docx`.
|
|
16
|
+
|
|
17
|
+
**Word input.** The `.docx` output is the same document with its SutonnyMJ text in
|
|
18
|
+
Unicode. Styles, paragraph spacing, alignment, page setup, tables, pictures, headers and
|
|
19
|
+
footers are left as they are; text is set at `--scale` of its size, with exact line
|
|
20
|
+
spacing equal to what SutonnyMJ gave, so lines and pages stay where they were. PDF output
|
|
21
|
+
lays that document out at its page size and margins (printed with Chrome; headers,
|
|
22
|
+
footers and footnotes are not drawn). EPUB output starts a chapter at each large bold
|
|
23
|
+
heading, with bold headings at body size as sections.
|
|
24
|
+
|
|
25
|
+
The outputs, for PDF or Word input:
|
|
26
|
+
|
|
27
|
+
| File | When | What it is |
|
|
28
|
+
|------|------|------------|
|
|
29
|
+
| `<name>_unicode.md` | `md` | Markdown following the layout: paragraphs, headings, bold and italic runs, centred lines and pictures (saved in `<name>_unicode_images/`). |
|
|
30
|
+
| `<name>_unicode.pdf` | `pdf` | Same page layout in a Unicode Bengali font (Noto Serif Bengali), with pictures and page colour kept. Text is searchable and copyable. |
|
|
31
|
+
| `<name>_unicode.docx` | `docx` | Word document with the source's formatting: page size, margins, page colour, line spacing, justified paragraphs with first-line indents, centred headings, framed pictures, bold and italic, page numbers. One page per source page. Fonts are embedded. |
|
|
32
|
+
| `<name>_unicode.epub` | `epub` | Reflowable EPUB 3 e-book: chapters split at headings, table of contents, justified paragraphs with indents, bold and italic, pictures, and the printed page numbers as a page list. Fonts are embedded. |
|
|
33
|
+
| `<name>_unicode.html` | `html` | The PDF's pages as one self-contained HTML file (fonts and pictures embedded). Open it in a browser and print to PDF; no Chrome install is needed. Print it once the page has finished loading, at 100% scale with no margins or headers. |
|
|
34
|
+
|
|
35
|
+
## Requirements
|
|
36
|
+
|
|
37
|
+
- Python 3.9 or newer
|
|
38
|
+
- [PyMuPDF](https://pymupdf.readthedocs.io/) (installed automatically)
|
|
39
|
+
- Google Chrome or Chromium, **only for PDF output**. Markdown, DOCX, EPUB and HTML output need no browser.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
From PyPI, into a virtual environment:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
python3 -m venv .venv
|
|
47
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
48
|
+
pip install ansibangla
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Or as a stand-alone command with [pipx](https://pipx.pypa.io/) or [uv](https://docs.astral.sh/uv/):
|
|
52
|
+
`pipx install ansibangla` or `uv tool install ansibangla`.
|
|
53
|
+
|
|
54
|
+
No install at all: the web app at <https://ansibangla.vercel.app/> runs the same converter
|
|
55
|
+
in your browser.
|
|
56
|
+
|
|
57
|
+
For development, from the `python/` folder of a checkout (code changes take effect
|
|
58
|
+
without reinstalling):
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
pip install -e . pytest
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Chrome / Chromium
|
|
65
|
+
|
|
66
|
+
The PDF output is laid out as HTML and printed with headless Chrome. The tool looks for,
|
|
67
|
+
in order:
|
|
68
|
+
|
|
69
|
+
1. the `--chrome` option (or `chrome=` argument in Python),
|
|
70
|
+
2. the `ANSIBANGLA_CHROME` environment variable,
|
|
71
|
+
3. `google-chrome`, `google-chrome-stable`, `chromium`, `chromium-browser` or `chrome` on your `PATH`.
|
|
72
|
+
|
|
73
|
+
On macOS and Windows Chrome is usually not on `PATH`, so point to it:
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
# macOS
|
|
77
|
+
export ANSIBANGLA_CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
|
|
78
|
+
|
|
79
|
+
# Windows (PowerShell)
|
|
80
|
+
$env:ANSIBANGLA_CHROME = "C:\Program Files\Google\Chrome\Application\chrome.exe"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
On Debian/Ubuntu, `sudo apt install chromium` (or install Google Chrome) is enough.
|
|
84
|
+
|
|
85
|
+
## Command line usage
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
ansibangla book.pdf # Unicode PDF into the current folder
|
|
89
|
+
ansibangla book.pdf -o out/ # into out/
|
|
90
|
+
ansibangla book.pdf -f docx # Word only (no Chrome needed)
|
|
91
|
+
ansibangla book.pdf -f pdf docx epub md # all four
|
|
92
|
+
ansibangla book.pdf -f pdf,docx # commas work too, as does -f pdf -f docx
|
|
93
|
+
ansibangla book.pdf -f epub --title 'বইয়ের নাম' # e-book with a title
|
|
94
|
+
ansibangla a.pdf b.pdf -f md # several files, Markdown only
|
|
95
|
+
ansibangla story.docx -o out/ # Word input: Unicode .docx
|
|
96
|
+
ansibangla story.docx -f pdf epub # Word input: PDF + e-book
|
|
97
|
+
ansibangla book.pdf --scale 0.85
|
|
98
|
+
ansibangla book.pdf --chrome /path/to/chrome
|
|
99
|
+
python -m ansibangla book.pdf # same, without the installed script
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Options:
|
|
103
|
+
|
|
104
|
+
| Option | Default | Meaning |
|
|
105
|
+
|--------|---------|---------|
|
|
106
|
+
| `-o`, `--out-dir` | `.` | Output folder (created if missing). |
|
|
107
|
+
| `-f`, `--formats` | the input's format | Outputs: any of `pdf`, `docx`, `epub`, `md`, `html`, separated by spaces or commas, or by repeating `-f`. Without it: `pdf` for PDFs, `docx` for `.docx` files. |
|
|
108
|
+
| `--scale` | `0.9` | Font-size factor for the Unicode PDF and DOCX (also for `.docx` input). Lower it if text looks crowded. |
|
|
109
|
+
| `--chrome` | auto | Path to Chrome or Chromium. |
|
|
110
|
+
| `--title` | first heading | Title of the EPUB. |
|
|
111
|
+
| `--keep-latin` | none | File listing English words typed in the SutonnyMJ font that stay as typed (one per line or space-separated; `#` starts a comment). Bijoy reads Latin letters as Bengali, so such words otherwise come out as Bengali gibberish. |
|
|
112
|
+
|
|
113
|
+
The command prints each written file's path. If one input fails, it reports the error,
|
|
114
|
+
carries on with the rest, and exits with status 1.
|
|
115
|
+
|
|
116
|
+
## Python usage
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from ansibangla import convert_file, convert_pdf, convert_text
|
|
120
|
+
|
|
121
|
+
paths = convert_pdf('book.pdf', 'out/')
|
|
122
|
+
# {'pdf': 'out/book_unicode.pdf'}
|
|
123
|
+
|
|
124
|
+
convert_pdf('book.pdf', 'out/', formats=('docx', 'md'), stem='chapter1')
|
|
125
|
+
# {'docx': 'out/chapter1_unicode.docx', 'md': 'out/chapter1_unicode.md'}
|
|
126
|
+
|
|
127
|
+
convert_file('story.docx', 'out/', formats=('pdf', 'epub')) # picks by extension
|
|
128
|
+
|
|
129
|
+
convert_text('wbwØ©avq') # 'নির্দ্বিধায়'
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`convert_pdf(src, out_dir='.', *, stem=None, formats=('pdf',), scale=0.9, font_dir=None, chrome=None, title=None)`
|
|
133
|
+
|
|
134
|
+
- `stem`: base name for the output files (default: input file name).
|
|
135
|
+
- `formats`: any combination of `'pdf'`, `'docx'`, `'epub'`, `'md'`, `'html'`.
|
|
136
|
+
- `scale`: font-size factor for the PDF and DOCX.
|
|
137
|
+
- `font_dir`: folder with replacement fonts (default: the bundled Noto Serif Bengali).
|
|
138
|
+
- `chrome`: path to Chrome or Chromium.
|
|
139
|
+
- `title`: EPUB title (default: the first heading).
|
|
140
|
+
- `keep_latin`: English words typed in the SutonnyMJ font that stay as typed.
|
|
141
|
+
|
|
142
|
+
`convert_docx(src, out_dir='.', *, stem=None, formats=('docx',), font_dir=None, scale=0.9, chrome=None, title=None)`
|
|
143
|
+
converts a Word document; `convert_file(src, out_dir, ...)` picks `convert_pdf` or
|
|
144
|
+
`convert_docx` by the file extension.
|
|
145
|
+
|
|
146
|
+
`convert_text(text)` converts a single SutonnyMJ string, as extracted from a PDF, to
|
|
147
|
+
Unicode. Useful for text you copied out of a Bijoy-typed document.
|
|
148
|
+
|
|
149
|
+
## How it works
|
|
150
|
+
|
|
151
|
+
- **Character mapping.** SutonnyMJ key codes are mapped to Unicode, pre-base vowel signs
|
|
152
|
+
(ি ে ৈ) and reph (র্) are moved into logical order, and glyphs that PDF extraction
|
|
153
|
+
mis-codes are fixed. English words typed in the same font are kept as typed when listed
|
|
154
|
+
with `--keep-latin`.
|
|
155
|
+
- **Layout measurement.** Body font size, left and right text edges, the page-number zone
|
|
156
|
+
and pictures are measured from the PDF itself. Nothing is hard-coded for one book.
|
|
157
|
+
- **PDF rebuild.** Body text is rebuilt as justified paragraphs. Each block is fitted in
|
|
158
|
+
Chrome to the same number of lines as its source, so text does not collide with
|
|
159
|
+
headings or English lines. Page breaks are kept.
|
|
160
|
+
- **DOCX rebuild.** The document is written directly as WordprocessingML, with no extra
|
|
161
|
+
package. Each source page starts a new page, and blocks keep their vertical positions
|
|
162
|
+
through measured spacing. A paragraph that runs on to the next page is split there.
|
|
163
|
+
The bundled fonts are embedded, so Word and LibreOffice show them even where they are
|
|
164
|
+
not installed.
|
|
165
|
+
- **EPUB build.** The e-book is reflowable: the reader's device breaks lines and pages,
|
|
166
|
+
so there is no line-breaking mismatch to manage. It keeps the structure: a chapter
|
|
167
|
+
starts at each large heading, with the centred line above it (such as "অধ্যায় ১") as its
|
|
168
|
+
opening. Bold subheadings at the left margin become section headings in the table of
|
|
169
|
+
contents. Extra vertical space in the source, such as the blank line above a subheading,
|
|
170
|
+
is measured from the line positions and kept. Each source page gets an invisible page marker, so readers that support page
|
|
171
|
+
lists can go to a printed page number. It is written directly with `zipfile` and
|
|
172
|
+
passes EPUBCheck.
|
|
173
|
+
- **Word input.** Word splits text into many runs (spell-check marks alone cut it every
|
|
174
|
+
few words). Runs with the same formatting are merged, and a word still split across
|
|
175
|
+
differently formatted runs is moved to one side, so the reordering sees whole words.
|
|
176
|
+
Only SutonnyMJ runs change: they get the Unicode font, and Word's complex-script size,
|
|
177
|
+
bold and italic (which Bengali Unicode is laid out with) are set to match. Zero-width
|
|
178
|
+
joiners typed into the Bijoy text are dropped, since they change how Unicode conjuncts
|
|
179
|
+
are drawn.
|
|
180
|
+
|
|
181
|
+
## Project layout
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
ansibangla/
|
|
185
|
+
bijoy.py SutonnyMJ -> Unicode mapping (MAP, PDF_FIXUPS) and reordering
|
|
186
|
+
_common.py reading PDF spans, lines, pictures and measuring page layout
|
|
187
|
+
blocks.py reading order as blocks (paragraphs, headings, pictures) for MD, DOCX, EPUB
|
|
188
|
+
mdexport.py Markdown output
|
|
189
|
+
docxexport.py Word (.docx) output
|
|
190
|
+
epubexport.py EPUB 3 e-book output
|
|
191
|
+
pdfbuild.py HTML layout -> printable HTML, or headless Chrome -> PDF output
|
|
192
|
+
docxinput.py Word (.docx) input: SutonnyMJ runs converted in place
|
|
193
|
+
docxrender.py PDF, HTML and EPUB from a converted Word document
|
|
194
|
+
api.py convert_file / convert_pdf / convert_docx / convert_text
|
|
195
|
+
cli.py command line
|
|
196
|
+
fonts/ Noto Serif Bengali W94 (OFL), sized to match SutonnyMJ
|
|
197
|
+
tests/ pytest suite
|
|
198
|
+
tools/
|
|
199
|
+
make_fonts.py rebuilds the bundled fonts from the variable Noto Serif Bengali
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Limits
|
|
203
|
+
|
|
204
|
+
- Line breaks in body text differ from the source, because Unicode Bengali is wider.
|
|
205
|
+
Page breaks are kept.
|
|
206
|
+
- Glyph mappings were checked against real SutonnyMJ books, not against the publisher's font.
|
|
207
|
+
**Proofread the output.**
|
|
208
|
+
- Only text set in a font whose name contains `MJ` is converted; other fonts pass through unchanged.
|
|
209
|
+
- Scanned PDFs (images of pages, no text layer) are not supported; use OCR for those.
|
|
210
|
+
- The Markdown has no justification or page numbers; it is for reading and editing.
|
|
211
|
+
- In the DOCX, text flows within each page, so line breaks differ from the source and a
|
|
212
|
+
page may end a line or two early. The last line before a paragraph splits across pages
|
|
213
|
+
is not justified. Page numbers use Latin digits (Word has no Bengali page-number format).
|
|
214
|
+
If a page overflows on to an extra page, lower `--scale`.
|
|
215
|
+
- The EPUB does not keep the page colour or exact positions, because e-readers apply their
|
|
216
|
+
own theme and layout. Readers that let you choose a font replace the embedded one.
|
|
217
|
+
|
|
218
|
+
### Fixing a wrong character
|
|
219
|
+
|
|
220
|
+
If some word comes out wrong, the fix is usually one entry in `ansibangla/bijoy.py`:
|
|
221
|
+
|
|
222
|
+
- `PDF_FIXUPS`: characters the PDF extractor reports wrongly, replaced before mapping.
|
|
223
|
+
- `MAP`: SutonnyMJ sequences and their Unicode output. Longer sequences must come first.
|
|
224
|
+
|
|
225
|
+
Find the raw text with PyMuPDF (`page.get_text('rawdict')`), add the entry, and add a
|
|
226
|
+
case to `tests/test_convert.py`.
|
|
227
|
+
|
|
228
|
+
## Tests
|
|
229
|
+
|
|
230
|
+
```sh
|
|
231
|
+
pip install -e . pytest
|
|
232
|
+
pytest
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
The sample-book tests run only when PDF or `.docx` files are present in `sample/` at the
|
|
236
|
+
top of the repository. That folder is ignored by git, because sample books are usually
|
|
237
|
+
copyrighted; put your own test files there.
|
|
238
|
+
|
|
239
|
+
## Fonts and licence
|
|
240
|
+
|
|
241
|
+
`ansibangla/fonts/` holds Noto Serif Bengali W94 (Regular and Bold), static instances of
|
|
242
|
+
Noto Serif Bengali, © The Noto Project Authors, licensed under the SIL Open Font
|
|
243
|
+
License 1.1. See `ansibangla/fonts/OFL.txt`.
|
|
244
|
+
|
|
245
|
+
They are cut to match SutonnyMJ's proportions. SutonnyMJ draws its letters about 11%
|
|
246
|
+
smaller than Unicode Bengali fonts at the same point size, so text is set at 0.9 of the
|
|
247
|
+
source size (`--scale`). At that size, Noto Serif Bengali's width axis at 94 gives the
|
|
248
|
+
same line widths as SutonnyMJ: 0.997 of the source width, measured on SutonnyMJ books.
|
|
249
|
+
For comparison, the regular width gives 1.036 and SemiCondensed gives 0.957. The
|
|
250
|
+
instances have their own family name, so an installed Noto Serif Bengali of another
|
|
251
|
+
width is never used in their place. One placement rule is also changed. With া, ো or
|
|
252
|
+
ৌ, Noto puts chandrabindu far left (দাঁ) or past the syllable (পৌঁ). The bundled fonts
|
|
253
|
+
centre it over the join between the letter and its vowel stroke, as SutonnyMJ does.
|
|
254
|
+
Everywhere else Noto's placement is kept. To rebuild them: `pip install fonttools && python
|
|
255
|
+
tools/make_fonts.py`.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""ansibangla: convert Bengali text, PDFs and Word files set in the SutonnyMJ (Bijoy) font to Unicode.
|
|
2
|
+
|
|
3
|
+
from ansibangla import convert_file, convert_pdf, convert_text
|
|
4
|
+
convert_file('story.docx', 'out/') # writes a Unicode .docx
|
|
5
|
+
convert_pdf('book.pdf', 'out/') # writes a Unicode .pdf
|
|
6
|
+
convert_pdf('book.pdf', 'out/', formats=('docx', 'epub', 'md')) # several at once
|
|
7
|
+
convert_text('wbwØ©avq') # -> 'নির্দ্বিধায়'
|
|
8
|
+
"""
|
|
9
|
+
from .api import DEFAULT_FORMATS, FORMATS, convert_docx, convert_file, convert_pdf, convert_text
|
|
10
|
+
from .bijoy import convert as bijoy_convert
|
|
11
|
+
|
|
12
|
+
__all__ = ['convert_file', 'convert_pdf', 'convert_docx', 'convert_text', 'bijoy_convert', 'FORMATS', 'DEFAULT_FORMATS']
|
|
13
|
+
__version__ = '1.0.0'
|