> 文章列表 > from PyPDF2.utils import PdfReadError报错

from PyPDF2.utils import PdfReadError报错

from PyPDF2.utils import PdfReadError报错

from PyPDF2.errors import PdfReadError

应该这样写 

def read_pdf(filename):try:filepath = os.path.join(pdf_files_path, filename)with open(filepath, 'rb') as f:pdf = PdfReader(f)text = ''for page in pdf.pages:text += page.extract_text() # 获取该页中的文本return text.strip() # 返回文本内容并去掉首尾空格except FileNotFoundError as e:return ""except PdfReadError as e:print(f"There was an error reading {filename}: {e}")return ""