Rebased-specific.
()
| 79 | sys.stdout.flush() |
| 80 | |
| 81 | def test_extract4(): |
| 82 | ''' |
| 83 | Rebased-specific. |
| 84 | ''' |
| 85 | if not hasattr(pymupdf, 'mupdf'): |
| 86 | return |
| 87 | path = f'{pymupdfdir}/tests/resources/2.pdf' |
| 88 | document = pymupdf.open(path) |
| 89 | page = document[4] |
| 90 | |
| 91 | out = 'test_stext.html' |
| 92 | text = page.get_text('html') |
| 93 | with open(out, 'w') as f: |
| 94 | f.write(text) |
| 95 | print(f'Have written to: {out}') |
| 96 | |
| 97 | out = 'test_extract.html' |
| 98 | writer = pymupdf.mupdf.FzDocumentWriter( |
| 99 | out, |
| 100 | 'html', |
| 101 | pymupdf.mupdf.FzDocumentWriter.OutputType_DOCX, |
| 102 | ) |
| 103 | device = pymupdf.mupdf.fz_begin_page(writer, pymupdf.mupdf.fz_bound_page(page)) |
| 104 | pymupdf.mupdf.fz_run_page(page, device, pymupdf.mupdf.FzMatrix(), pymupdf.mupdf.FzCookie()) |
| 105 | pymupdf.mupdf.fz_end_page(writer) |
| 106 | pymupdf.mupdf.fz_close_document_writer(writer) |
| 107 | print(f'Have written to: {out}') |
| 108 | |
| 109 | def get_text(page, space_guess): |
| 110 | buffer_ = pymupdf.mupdf.FzBuffer( 10) |
| 111 | out = pymupdf.mupdf.FzOutput( buffer_) |
| 112 | writer = pymupdf.mupdf.FzDocumentWriter( |
| 113 | out, |
| 114 | f'text,space-guess={space_guess}', |
| 115 | pymupdf.mupdf.FzDocumentWriter.OutputType_DOCX, |
| 116 | ) |
| 117 | device = pymupdf.mupdf.fz_begin_page(writer, pymupdf.mupdf.fz_bound_page(page)) |
| 118 | pymupdf.mupdf.fz_run_page(page, device, pymupdf.mupdf.FzMatrix(), pymupdf.mupdf.FzCookie()) |
| 119 | pymupdf.mupdf.fz_end_page(writer) |
| 120 | pymupdf.mupdf.fz_close_document_writer(writer) |
| 121 | text = buffer_.fz_buffer_extract() |
| 122 | text = text.decode('utf8') |
| 123 | n = text.count(' ') |
| 124 | print(f'{space_guess=}: {n=}') |
| 125 | return text, n |
| 126 | page = document[4] |
| 127 | text0, n0 = get_text(page, 0) |
| 128 | text1, n1 = get_text(page, 0.5) |
| 129 | text2, n2 = get_text(page, 0.001) |
| 130 | text2, n2 = get_text(page, 0.1) |
| 131 | text2, n2 = get_text(page, 0.3) |
| 132 | text2, n2 = get_text(page, 0.9) |
| 133 | text2, n2 = get_text(page, 5.9) |
| 134 | assert text1 == text0 |
| 135 | |
| 136 | def test_2954(): |
| 137 | ''' |