From 6dd4312d846845929eabd221e0ba256ec86a763c Mon Sep 17 00:00:00 2001 From: Infernio Date: Fri, 13 Sep 2024 21:17:56 +0200 Subject: [PATCH] pdfviewer: Add support for pymupdf renaming The 'fitz' module got renamed to 'pymupdf' in 1.24.3 Prepare for an eventual deprecation and removal by importing from pymupdf first and falling back to fitz only if that fails. I also got rid of the other 'fitz'es in the file via 'fitz as pymupdf', since pymupdf.open is much easier to read than fitz.open. --- wx/lib/pdfviewer/viewer.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/wx/lib/pdfviewer/viewer.py b/wx/lib/pdfviewer/viewer.py index 4c41cfba9..ee66f7236 100644 --- a/wx/lib/pdfviewer/viewer.py +++ b/wx/lib/pdfviewer/viewer.py @@ -38,7 +38,10 @@ try: # see http://pythonhosted.org/PyMuPDF - documentation & installation - import fitz + try: + import pymupdf + except ImportError: + import fitz as pymupdf mupdf = True if VERBOSE: print('pdfviewer using PyMuPDF (GPL)') except ImportError: @@ -474,7 +477,7 @@ class mupdfProcessor(object): """ Create an instance of this class to open a PDF file, process the contents of each page and render each one on demand using the GPL mupdf library, which is - accessed via the python-fitz package bindings (version 1.9.1 or later) + accessed via the pymupdf package bindings (version 1.9.1 or later) """ def __init__(self, parent, pdf_file): """ @@ -484,17 +487,17 @@ def __init__(self, parent, pdf_file): """ self.parent = parent if isinstance(pdf_file, string_types): - # a filename/path string, pass the name to fitz.open + # a filename/path string, pass the name to pymupdf.open pathname = pdf_file - self.pdfdoc = fitz.open(pathname) + self.pdfdoc = pymupdf.open(pathname) else: - # assume it is a file-like object, pass the stream content to fitz.open + # assume it is a file-like object, pass the stream content to pymupdf.open # and a '.pdf' extension in pathname to identify the stream type pathname = 'fileobject.pdf' if pdf_file.tell() > 0: # not positioned at start pdf_file.seek(0) stream = bytearray(pdf_file.read()) - self.pdfdoc = fitz.open(pathname, stream) + self.pdfdoc = pymupdf.open(pathname, stream) try: self.numpages = self.pdfdoc.page_count @@ -522,7 +525,7 @@ def RenderPage(self, gc, pageno, scale=1.0): page = self.pdfdoc.load_page(pageno) except AttributeError: # old PyMuPDF version page = self.pdfdoc.loadPage(pageno) - matrix = fitz.Matrix(scale, scale) + matrix = pymupdf.Matrix(scale, scale) try: try: # MUST be keyword arg(s)