I have installed PymuPDF/fitz because am trying to extract images from PDF files. However, upon running the code below, I am seeing No module named 'frontend'
.
doc = fitz.open(pdf_path)for i in range(len(doc)):for img in doc.getPageImageList(i):xref = img[0]pix = fitz.Pixmap(doc, xref)if pix.n < 5: # this is GRAY or RGBpix.writePNG("p%s-%s.png" % (i, xref))else: # CMYK: convert to RGB firstpix1 = fitz.Pixmap(fitz.csRGB, pix)pix1.writePNG("p%s-%s.png" % (i, xref))pix1 = Nonepix = None
I have searched but there isn't single report of this kind of error. I have installed PyMuPDF, muPDF and fitz modules
Here is the error in full:
Traceback (most recent call last):File "/home/waqar/PycharmProjects/predator/ExtractFileImage.py", line 1, in <module>import fitzFile "/home/waqar/anaconda3/envs/retinanet/lib/python3.6/site-packages/fitz/__init__.py", line 1, in <module>from frontend import *ModuleNotFoundError: No module named 'frontend'
Best Answer
I've solved it by:
pip install PyMuPDF
This will actually allow the import of the fitz
you appear to want. (There's another fitz, which is probably not what you want if you're manipulating PDF files.)
NOTE: If you get RuntimeError: Directory 'static/' does not exist
after install than do:
pip uninstall fitz
for more info see: raise RuntimeError(f"Directory '{directory}' does not exist") RuntimeError: Directory 'static/' does not exist from import fitz
I tried the above solution of pip install PyMuPDF
.But it did not work out of the box.
So, I have used the previous version of PyMuPDF. It worked perfectly for me.
pip install PyMuPDF==1.16.14
There is a package named fitz
on PyPI. Because PyMuPDF uses the same name, fitz
, as its top-level text, both packages cannot co-exist in the same Python - except with the aforementioned change.
Python3 and you have already installed PyMuPDF module.
pip install --upgrade pippip install -U PyMuPDF
You should run pip install fitz
follow by pip install PyMuPDF
. If you have install PyMuPDF, uninstall it and install again.
In file /home/waqar/anaconda3/envs/retinanet/lib/python3.6/site-packages/fitz/__init__.py
change
from frontend
to from fitz.frontend
- pip install PyMuPDF
- If the above command has the error of fitz.h, then upgrade the python version, I found that python3.6.* will have this problem. If you cannot upgrade the python version, lower the version of pymupdf (version 1.18.0 is OK, pip install PyMuPDF==1.18.0).
Calling python on your script should solve the issue:
python script.py
If you don't use the keyword python, you might get the error.
In my case I was getting:
ModuleNotFoundError: No module named 'fitz'
This combo works fine for me:
sudo apt install mupdfsudo apt install libmupdf-devpip3 install PyMuPDF==1.16
You could have used pdfplumber. If the following code returns "None", it's a scanned pdf otherwise it's searchable.
with pdfplumber.open(file_name) as pdf:page = pdf.pages[0]text = page.extract_text()print(text)
To extract text from scanned pdf, you can use OCRmyPDF. Easy package