ImportError: cannot import name 'Document' from 'borb.pdf.document

2.5k views Asked by At

I have installed the 'borb' library using pip install borb and after installation, I got the following message:

Requirement already satisfied: borb in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (2.0.25)
WARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.
You should consider upgrading via the 'c:\users\dell\appdata\local\programs\python\python37\python.exe -m pip install --upgrade pip' command.

Requirement already satisfied: qrcode[pil]>=6.1 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from borb) (7.3.1)
Requirement already satisfied: Pillow>=7.1.0 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from borb) (9.0.0)
Requirement already satisfied: requests>=2.24.0 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from borb) (2.25.1)
Requirement already satisfied: setuptools>=51.1.1 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from borb) (54.2.0)
Requirement already satisfied: fonttools>=4.22.1 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from borb) (4.33.3)
Requirement already satisfied: python-barcode>=0.13.1 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from borb) (0.13.1)
Requirement already satisfied: colorama in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from qrcode[pil]>=6.1->borb) (0.4.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from requests>=2.24.0->borb) (2020.12.5)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from requests>=2.24.0->borb) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from requests>=2.24.0->borb) (2.10)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from requests>=2.24.0->borb) (1.26.4)

Now, if I run the following code snippet I am getting an ImportError:

import typing  
from borb.pdf.document import Document  
from borb.pdf.pdf import PDF  
from borb.toolkit.text.simple_text_extraction import SimpleTextExtraction  


def main():

    # variable to hold Document instance
    doc: typing.Optional[Document] = None  

    # this implementation of EventListener handles text-rendering instructions
    l: SimpleTextExtraction = SimpleTextExtraction()  

    # open the document, passing along the array of listeners
    with open("PDFs/1.pdf", "rb") as in_file_handle:
        doc = PDF.loads(in_file_handle, [l])  
  
    # were we able to read the document?
    assert doc is not None  

    # print the text on page 0
    print(l.get_text(0))  

if __name__ == "__main__":
    main()

I get the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5400/405389204.py in <module>
      1 import typing
----> 2 from borb.pdf.document import Document
      3 from borb.pdf.pdf import PDF
      4 from borb.toolkit.text.simple_text_extraction import SimpleTextExtraction
      5 

ImportError: cannot import name 'Document' from 'borb.pdf.document' (C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\borb\pdf\document\__init__.py)

Any inputs on how to resolve this...?

2

There are 2 answers

0
Akoffice On BEST ANSWER

Please import Document Class from borb.pdf
usage: from borb.pdf import Document

1
user20718136 On
from borb.pdf.document.document import Document

"Document" is a class from borb.pdf.document.document.py