Mirror of biological-variation 1.0.0 (Zenodo doi:10.5281/zenodo.24543299, GitHub caraulani/biological-variation)
62427f9 verified Download scripts/pdftext.py from lifefrom/biological-variation: direct link, hf CLI and curl.
- Browser
- Download file 645 Bytes
-
https://huggingface.co/datasets/lifefrom/biological-variation/resolve/main/scripts/pdftext.py
- Command line
-
hf download hf://datasets/lifefrom/biological-variation/scripts/pdftext.py
-
curl -L -o pdftext.py https://huggingface.co/datasets/lifefrom/biological-variation/resolve/main/scripts/pdftext.py
645 Bytes
| """Extract text from a PDF page by page. De Gruyter PDFs set digits in a font that maps | |
| them to private-use code points U+F130..U+F139; these are mapped back to 0..9.""" | |
| import sys | |
| import pymupdf | |
| PUA = {0xF130 + d: str(d) for d in range(10)} | |
| def pdf_to_text(path): | |
| doc = pymupdf.open(path) | |
| out = [] | |
| for i, page in enumerate(doc, 1): | |
| t = page.get_text("text", sort=True, flags=pymupdf.TEXT_CID_FOR_UNKNOWN_UNICODE | pymupdf.TEXT_PRESERVE_WHITESPACE) | |
| out.append(f"\n=== PAGE {i} ===\n" + t.translate(PUA)) | |
| return "".join(out) | |
| if __name__ == "__main__": | |
| open(sys.argv[2], "w").write(pdf_to_text(sys.argv[1])) | |