|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Open Arbitrary Files as TXT Document\n", |
| 8 | + "\n", |
| 9 | + "PyMuPDF can open plain text files as a `fitz.Document`. Because of its support for UTF-8 (and UTF-16) endcoding, a wide range of files can be opened as if they were text files.\n", |
| 10 | + "\n", |
| 11 | + "Here is a small example where we create a PDF and then open it again as a text file.\n", |
| 12 | + "\n", |
| 13 | + "> **NOTE:** Files containing a NULL byte will currently not be processed completely, as this is regarded as an EOF marker." |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 24, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [ |
| 21 | + { |
| 22 | + "name": "stdout", |
| 23 | + "output_type": "stream", |
| 24 | + "text": [ |
| 25 | + "%PDF-1.7\n", |
| 26 | + "%µ¶\n", |
| 27 | + "1 0 obj\n", |
| 28 | + "<</Type/Catalog/Pages 2 0 R>>\n", |
| 29 | + "endobj\n", |
| 30 | + "2 0 obj\n", |
| 31 | + "<</Type/Pages/Count 1/Kids[3 0 R]>>\n", |
| 32 | + "endobj\n", |
| 33 | + "3 0 obj\n", |
| 34 | + "<</Type/Page/MediaBox[0 0 595 842]/Rotate 0/Resources<</Font<</helv 4 0 R>>>>/Parent 2 0 R/Contents 5 0 R>>\n", |
| 35 | + "endobj\n", |
| 36 | + "4 0 obj\n", |
| 37 | + "<</Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>\n", |
| 38 | + "endobj\n", |
| 39 | + "5 0 obj\n", |
| 40 | + "<</Length 81>>\n", |
| 41 | + "stream\n", |
| 42 | + "q\n", |
| 43 | + "BT\n", |
| 44 | + "/helv 11 Tf\n", |
| 45 | + "1 0 0 1 100 742 Tm\n", |
| 46 | + "[(Just some arbitrary content.)] TJ\n", |
| 47 | + "ET\n", |
| 48 | + "Q\n", |
| 49 | + "q\n", |
| 50 | + "Q\n", |
| 51 | + "endstream\n", |
| 52 | + "endobj\n", |
| 53 | + "xref\n", |
| 54 | + "0 6\n", |
| 55 | + "0000000000 65536 f \n", |
| 56 | + "0000000016 00000 n \n", |
| 57 | + "0000000062 00000 n \n", |
| 58 | + "0000000114 00000 n \n", |
| 59 | + "0000000238 00000 n \n", |
| 60 | + "0000000327 00000 n \n", |
| 61 | + "trailer\n", |
| 62 | + "<</Size 6/Root 1 0 R/ID[<CC2A084CB9885ADABC51CE10CFC725E8><8F2C15D6C784DF5A97728DB5403FCAB8>]>>\n", |
| 63 | + "startxref\n", |
| 64 | + "457\n", |
| 65 | + "%%EOF\n", |
| 66 | + "\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "import fitz\n", |
| 72 | + "\n", |
| 73 | + "doc = fitz.open() # make a new empty PDF\n", |
| 74 | + "page = doc.new_page() # give it a page ... with some content\n", |
| 75 | + "page.insert_text((100, 100), \"Just some arbitrary content.\")\n", |
| 76 | + "page.clean_contents()\n", |
| 77 | + "# save the PDF to memory\n", |
| 78 | + "doc.save(\"test.pdf\", garbage=4, expand=True) # prevent premature EOF (0x00)\n", |
| 79 | + "doc.close()\n", |
| 80 | + "\n", |
| 81 | + "# make a TXT Document from the PDF\n", |
| 82 | + "doc = fitz.open(\"test.pdf\", filetype=\"txt\", rect=fitz.paper_rect(\"letter\"), fontsize=8)\n", |
| 83 | + "page = doc[0]\n", |
| 84 | + "print(page.get_text())" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "metadata": { |
| 89 | + "kernelspec": { |
| 90 | + "display_name": "Python 3", |
| 91 | + "language": "python", |
| 92 | + "name": "python3" |
| 93 | + }, |
| 94 | + "language_info": { |
| 95 | + "codemirror_mode": { |
| 96 | + "name": "ipython", |
| 97 | + "version": 3 |
| 98 | + }, |
| 99 | + "file_extension": ".py", |
| 100 | + "mimetype": "text/x-python", |
| 101 | + "name": "python", |
| 102 | + "nbconvert_exporter": "python", |
| 103 | + "pygments_lexer": "ipython3", |
| 104 | + "version": "3.12.0" |
| 105 | + } |
| 106 | + }, |
| 107 | + "nbformat": 4, |
| 108 | + "nbformat_minor": 2 |
| 109 | +} |
0 commit comments