|

楼主 |
发表于 2023-7-10 12:17:12
|
显示全部楼层
本帖最后由 ssqchina 于 2023-7-10 12:29 编辑
import os
from PyPDF2 import PdfFileReader #类名用大写字母开头
def search_keywords_in_pdf(directory, keyword):
files = os.listdir(directory)
for file in files:
if file.endswith('.pdf'):
file_path = os.path.join(directory, file)
with open(file_path, 'rb') as f:
pdf = PdfFileReader(f)
for page_num in range(pdf.getNumPages()):
page = pdf.getPage(page_num)
text = page.extractText()
if keyword in text:
print(f"Keyword '{keyword}' found in {file} (Page {page_num + 1})")
search_keywords_in_pdf('E:\\报表\\','工资')
"C:\Program Files (x86)\Python39-32\python.exe" D:\Python\PDF文档搜索.py
Traceback (most recent call last):
File "D:\Python\PDF文档搜索.py", line 17, in <module>
search_keywords_in_pdf('E:\\报表\\','工资')
File "D:\Python\PDF文档搜索.py", line 10, in search_keywords_in_pdf
pdf = PdfFileReader(f)
File "C:\Users\ssq\AppData\Roaming\Python\Python39\site-packages\PyPDF2\_reader.py", line 1974, in __init__
deprecation_with_replacement("PdfFileReader", "PdfReader", "3.0.0")
File "C:\Users\ssq\AppData\Roaming\Python\Python39\site-packages\PyPDF2\_utils.py", line 369, in deprecation_with_replacement
deprecation(DEPR_MSG_HAPPENED.format(old_name, removed_in, new_name))
File "C:\Users\ssq\AppData\Roaming\Python\Python39\site-packages\PyPDF2\_utils.py", line 351, in deprecation
raise DeprecationError(msg)
PyPDF2.errors.DeprecationError: PdfFileReader is deprecated and was removed in PyPDF2 3.0.0. Use PdfReader instead.
进程已结束,退出代码1 |
|