Forum Discussion

aby_29's avatar
aby_29
New Member
1 year ago
Solved

Import PDF in Fabric Notebook

Hi everyone,

 

I'm looking to extract some data from some PDF files that are stored in a lakehouse. I would ideally like to store the extracted data as tables in the lakehouse. Is there a way to import PDF files into a Fabric PySpark notebook? I want to do some processing on the extracted data and write it back to the lakehouse (hence why I'm using Notebooks).

 

I'm very new to Fabric so any help would be appreciated.  

 

Thanks!

8 Replies

  • aby_29 

    this approach worked for me, Once data is in DF you can do anything with it.

     

    %pip install PyPDF2
    from io import BytesIO
    import PyPDF2
    binary_df = spark.read.format("binaryFile").load("Files/MyFolder/sample.pdf").limit(1)
    pdf_bytes = binary_df.collect()[0].content

    file_stream = BytesIO(pdf_bytes)
    reader = PyPDF2.PdfReader(file_stream)

    extracted_text = ""
    for page in reader.pages:
    extracted_text += page.extract_text()

     

    data_rows = [{"content": extracted_text}]
    df = spark.createDataFrame(data_rows)

    df.write.mode("overwrite").format("delta").saveAsTable("ExtractedPdfData")

     

     

  • Hi nilendraFabric , thank you for your response. 

     

     I am running into this error: FileNotFoundError: [Errno 2] No such file or directory: 'Files/sample.pdf' despite connecting the Notebook to the lakehouse and inserting the correct document name and path. Any idea why this might be the case and how I could fix it? 

    • nilendraFabric's avatar
      nilendraFabric
      Icon for Super User rankSuper User

       

       

      You have to attach a Lakehouse to you notebook and upload your file to File section.

      • aby_29's avatar
        aby_29
        New Member

        Hi nilendraFabric , please refer to the screenshot attached here

         

         

        The PDF file is in the abi_doc_int lakehouse

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi aby_29,

    Thanks for reaching out to the Microsoft fabric community forum.

     

    It looks like you were able to resolve your issues. If it so then kindly accept it as solution. If not then please tell us the issue you are still facing.

     

    I would also take a moment to thank nilendraFabric, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

     

    If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

    Best Regards,
    Hammad.
    Community Support Team

     

    If this post helps then please mark it as a solution, so that other members find it more quickly.

    Thank you.