要提取PDF文件的内容,可以使用Python内置的模块PyPDF2
。以下是一个示例代码:
import PyPDF2
def extract_text_from_pdf(file_path):
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text = ''
for page in pdf_reader.pages:
text += page.extract_text()
return text
# 使用示例
pdf_file_path = 'path/to/your/pdf/file.pdf'
extracted_text = extract_text_from_pdf(pdf_file_path)
print(extracted_text)
此代码使用PyPDF2
模块打开PDF文件,并遍历每个页面,提取文本内容。请确保将path/to/your/pdf/file.pdf
替换为实际的PDF文件路径。
请注意,PyPDF2
只能提取文本内容,不能提取图像或其他非文本元素。如果需要提取其他类型的数据,你可能需要使用其他库或工具。