comparison tests/test_pagedelete.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 """
2 ----------------------------------------------------
3 This tests correct functioning of multi-page delete
4 ----------------------------------------------------
5 Create a PDF in memory with 100 pages with a unique text each.
6 Also create a TOC with a bookmark per page.
7 On every page after the first to-be-deleted page, also insert a link, which
8 points to this page.
9 The bookmark text equals the text on the page for easy verification.
10
11 Then delete some pages and verify:
12 - the new TOC has empty items exactly for every deleted page
13 - the remaining TOC items still point to the correct page
14 - the document has no more links at all
15 """
16
17 import os
18
19 import pymupdf
20
21 scriptdir = os.path.dirname(__file__)
22 page_count = 100 # initial document length
23 r = range(5, 35, 5) # contains page numbers we will delete
24 # insert this link on pages after first deleted one
25 link = {
26 "from": pymupdf.Rect(100, 100, 120, 120),
27 "kind": pymupdf.LINK_GOTO,
28 "page": r[0],
29 "to": pymupdf.Point(100, 100),
30 }
31
32
33 def test_deletion():
34 # First prepare the document.
35 doc = pymupdf.open()
36 toc = []
37 for i in range(page_count):
38 page = doc.new_page() # make a page
39 page.insert_text((100, 100), "%i" % i) # insert unique text
40 if i > r[0]: # insert a link
41 page.insert_link(link)
42 toc.append([1, "%i" % i, i + 1]) # TOC bookmark to this page
43
44 doc.set_toc(toc) # insert the TOC
45 assert doc.has_links() # check we did insert links
46
47 # Test page deletion.
48 # Delete pages in range and verify result
49 del doc[r]
50 assert not doc.has_links() # verify all links have gone
51 assert doc.page_count == page_count - len(r) # correct number deleted?
52 toc_new = doc.get_toc() # this is the modified TOC
53 # verify number of emptied items (have page number -1)
54 assert len([item for item in toc_new if item[-1] == -1]) == len(r)
55 # Deleted page numbers must correspond to TOC items with page number -1.
56 for i in r:
57 assert toc_new[i][-1] == -1
58 # Remaining pages must be correctly pointed to by the non-empty TOC items
59 for item in toc_new:
60 pno = item[-1]
61 if pno == -1: # one of the emptied items
62 continue
63 pno -= 1 # PDF page number
64 text = doc[pno].get_text().replace("\n", "")
65 # toc text must equal text on page
66 assert text == item[1]
67
68 doc.delete_page(0) # just for the coverage stats
69 del doc[5:10]
70 doc.select(range(doc.page_count))
71 doc.copy_page(0)
72 doc.move_page(0)
73 doc.fullcopy_page(0)
74
75
76 def test_3094():
77 path = os.path.abspath(f"{__file__}/../../tests/resources/test_2871.pdf")
78 document = pymupdf.open(path)
79 pnos = [i for i in range(0, document.page_count, 2)]
80 document.delete_pages(pnos)
81
82
83 def test_3150():
84 """Assert correct functioning for problem file.
85
86 Implicitly also check use of new MuPDF function
87 pdf_rearrange_pages() since version 1.23.9.
88 """
89 filename = os.path.join(scriptdir, "resources", "test-3150.pdf")
90 pages = [3, 3, 3, 2, 3, 1, 0, 0]
91 doc = pymupdf.open(filename)
92 doc.select(pages)
93 assert doc.page_count == len(pages)
94
95
96 def test_4462():
97 path0 = os.path.normpath(f'{__file__}/../../tests/resources/test_4462_0.pdf')
98 path1 = os.path.normpath(f'{__file__}/../../tests/resources/test_4462_1.pdf')
99 path2 = os.path.normpath(f'{__file__}/../../tests/resources/test_4462_2.pdf')
100 with pymupdf.open() as document:
101 document.new_page()
102 document.new_page()
103 document.new_page()
104 document.new_page()
105 document.save(path0)
106 with pymupdf.open(path0) as document:
107 assert len(document) == 4
108 document.delete_page(-1)
109 document.save(path1)
110 with pymupdf.open(path1) as document:
111 assert len(document) == 3
112 document.delete_pages(-1)
113 document.save(path2)
114 with pymupdf.open(path2) as document:
115 assert len(document) == 2