skip to Main Content

After writing this code in PyCharm i faced some errors with import statements. I have already installed PyPDF2-1.28.6, and other libraries. Can anyone explain please why this error is happening? How can i fix this error?

import logging
import os
from concurrent.futures import ThreadPoolExecutor

import aiofiles
from telegram import Update
from telegram.ext import Application, CommandHandler, CallbackContext, MessageHandler
from PyPDF2 import PdfFileReader, PdfFileWriter, pageobject
from reportlab.pdfgen import canvas

async def signature_page(signature: str) -> pageobject:
    c = canvas.Canvas(TEMP_SIGNATURE)
    c.drawString(100, 100, signature)
    c.save()
    async with aiofiles.open(TEMP_SIGNATURE, 'rb') as file:
        signature_page = pageobject(await file.read())
    return signature_page

if __name__ == "__main__":
    import asyncio

    asyncio.run(main())

2

Answers


  1. I quickly checked the code of the PyPDF2 package. It seems like you are trying to import PageObject, but in your code it is written in lower case.
    You need to replace all pageobject words with PageObject.

    Login or Signup to reply.
    1. PyPDF2 is deprecated. Use pypdf
    2. PageObject. Capitalization matters.
    3. There is no point in using async / await when reading pages with pypdf. Besides that, I don’t think you should construct PageObjects yourself.

    You can iterate over pages like that:

    from pypdf import PdfReader
    
    reader = PdfReader
    for page in reader.pages:
        print(page.extract_text())  # or whatever you want to achieve
    

    Also, it would have been good to note in the question what you want to achieve.

    Can anyone explain please why this error is happening? How can i fix this error?

    You’re importing something that doesn’t exist. Fix it by checking the right import location and using correct capitialization.

    As a beginner, you should avoid using async/await. It just makes things more complicated. First, make things work. Then optimize if necessary.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search