PyMuPDF:AttributeError: module 'fitz' has no attribute 'open'

9

pip3 install PyMuPDF

Collecting PyMuPDF Using cached PyMuPDF-1.18.17-cp37-cp37m-win_amd64.whl (5.4 MB)
Installing collected packages: PyMuPDF
Successfully installed PyMuPDF-1.18.17

import fitz
doc = fitz.open("my_pdf.pdf")

当我在文件 fitz.py 中查找 def open 时,没有找到任何内容。因此,我可以理解错误,但我不明白为什么我下载的文件没有这个函数?请问有人能分享正确的文件吗?或者说,我可能错过了其他什么东西吗?

完整追踪如下:

runfile('D:/Documents/Python_projects/Point_and_area_pdf_to_excel/get_info.py', wdir='D:/Documents/Python_projects/Point_and_area_pdf_to_excel')
Reloaded modules: six, dateutil._common, dateutil.relativedelta, dateutil.tz._common, dateutil.tz._factories, dateutil.tz.win, dateutil.tz.tz, dateutil.tz, dateutil.parser._parser, dateutil.parser.isoparser, dateutil.parser, chardet.enums, chardet.charsetprober, chardet.charsetgroupprober, chardet.codingstatemachine, chardet.escsm, chardet.escprober, chardet.latin1prober, chardet.mbcssm, chardet.utf8prober, chardet.mbcharsetprober, chardet.euctwfreq, chardet.euckrfreq, chardet.gb2312freq, chardet.big5freq, chardet.jisfreq, chardet.chardistribution, chardet.jpcntx, chardet.sjisprober, chardet.eucjpprober, chardet.gb2312prober, chardet.euckrprober, chardet.cp949prober, chardet.big5prober, chardet.euctwprober, chardet.mbcsgroupprober, chardet.hebrewprober, chardet.sbcharsetprober, chardet.langbulgarianmodel, chardet.langgreekmodel, chardet.langhebrewmodel, chardet.langrussianmodel, chardet.langthaimodel, chardet.langturkishmodel, chardet.sbcsgroupprober, chardet.universaldetector, chardet.version, chardet
Traceback (most recent call last):

  File "D:\Documents\Python_projects\Point_and_area_pdf_to_excel\get_info.py", line 45, in <module>
    print(get_dict_list(path))

  File "D:\Documents\Python_projects\Point_and_area_pdf_to_excel\get_info.py", line 7, in get_dict_list
    text_list = get_pdf_page_text_list(pdf_path)

  File "D:\Documents\Python_projects\Point_and_area_pdf_to_excel\get_info.py", line 19, in get_pdf_page_text_list
    doc = fitz.open(pdf_path)

AttributeError: module 'fitz' has no attribute 'open'

你能发布错误和跟踪吗? - The Singularity
这就是你需要的吗? - Utopion
3个回答

9

卸载并重新安装pyMuPDF。

这个错误通常表明init.py没有被执行。例如,当您在fitz安装目录中并启动python时会发生这种情况。

> pip uninstall PyMuPDF
Found existing installation: PyMuPDF 1.18.17
Uninstalling PyMuPDF-1.18.17:
  Would remove:
    /home/deerawj/.local/lib/python3.9/site-packages/PyMuPDF-1.18.17.dist-info/*
    /home/deerawj/.local/lib/python3.9/site-packages/fitz/*
Proceed (y/n)? y
  Successfully uninstalled PyMuPDF-1.18.17

> pip install PyMuPDF
Collecting PyMuPDF
  Downloading PyMuPDF-1.18.17-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (6.4 MB)
     |████████████████████████████████| 6.4 MB 62 kB/s
Installing collected packages: PyMuPDF
Successfully installed PyMuPDF-1.18.17

另外也请查看: https://github.com/pymupdf/PyMuPDF/issues/660

5
这很可能是一个安装问题,看起来你的环境中已经安装了一个名为“fitz”的包,而这与“PyMuPDF”无关。
因此,当“PyMuPDF”调用“fitz”时,它实际上可能会调用错误的“fitz”包。
你可以考虑进行所有依赖项的清洁安装或创建一个虚拟环境来使用“PyMuPDF”。
你也可以尝试将“fitz”回滚到版本“1.16.14”。

1
我认为是这个。在我的Spyder环境中,我无法解决这个问题,它太复杂了。我换到PyCharm环境后,它就像魔术般地工作了。 - Utopion

4
我也遇到了同样的问题。不得不移除Fitz,因为它是一个与PyMuPdf调用的不同的包。然后即使卸载了Fitz并安装了pymupdf,仍然出现相同的错误。 以下是对我有效的解决方法。
pip uninstall fitz
pip install --upgrade --force-reinstall pymupdf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接