1.pdfplumber安装,PDF文字提取

发布时间:2026/6/4 1:54:16

1.pdfplumber安装,PDF文字提取 一pdfplumber 库安装pip install pdfplumber-i https://pypi.tuna.tsinghua.edu.cn/simple二核心API详解学习pdfplumber.open()的核心参数文件路径、密码加密 PDF、页面范围等学习Page对象的核心方法extract_text()提取页面内的所有文本内容extract_words()提取单个单词 / 文本块带位置坐标信息extract_tables()提取页面内的表格内容本次学习仅做了解聚焦文本提取学习文本提取的常见参数x_tolerance/y_tolerance 文本块合并容差、keep_blank_chars 保留空白字符等学习多页 PDF 的遍历逻辑通过for page in pdf.pages 循环遍历所有页面批量提取全量文本三实操代码# -*- coding: utf-8 -*- Created on 2026/6/2 13:14 creator er_nao File day_85.py Description pdfplumber 安装与 PDF 文字提取 importpdfplumber# 一. 基础单页 PDF 文本提取pdf_file_path1C:\\Users\\hp\\Desktop\\NLP学习数据\\arctle1.pdfoutput_txt_path1C:\\Users\\hp\\Desktop\\NLP学习数据\\arctle1.txtpdf_file_path2C:\\Users\\hp\\Desktop\\NLP学习数据\\arctle2.pdfoutput_txt_path2C:\\Users\\hp\\Desktop\\NLP学习数据\\arctle2.txtdefextract_single_page_pdf(pdf_path,output_path): 提取单页PDF的全部文本内容并保存到TXT文件 :param pdf_path: PDF文件路径 :param output_path: 提取结果保存路径 # 1. 打开PDF文件with语句会自动关闭文件避免资源泄漏withpdfplumber.open(pdf_path)aspdf:# 2. 获取第一页单页PDF直接取第0个索引first_pagepdf.pages[0]# 3. 提取页面全部文本内容# 核心参数说明# x_tolerance/y_tolerance文本块合并容差数值越大越容易把相邻文本合并为一行# keep_blank_chars是否保留空白字符默认Falsefull_textfirst_page.extract_text(x_tolerance3,y_tolerance3,keep_blank_charsFalse)# 4. 打印提取结果控制台预览print( 单页PDF提取结果 )print(full_text)print()# 5. 将提取结果保存到TXT文件withopen(output_path,w,encodingutf-8)asf:f.write(full_text)print(f提取结果已保存至{output_path})# 二. 多页 PDF 全量文本提取defextract_multiple_page_pdfs(pdf_path,output_path): 提取多页PDF的全部文本内容按页码标注保存为结构化文件 :param pdf_path: PDF文件路径 :param output_path: 提取结果保存路径 # 1. 打开PDF文件withpdfplumber.open(pdf_path)aspdf:# 2. 初始化结果列表按页码存储all_page_text[]total_pageslen(pdf.pages)print(fPDF总页数{total_pages},开始提取)# 3. 循环遍历所有页面批量提取forpage_index,pageinenumerate(pdf.pages):# 页码从1开始标注符合阅读习惯page_numpage_index1# 提取当前页文本page_textpage.extract_text(x_tolerance3,y_tolerance3,keep_blank_charsFalse)# 处理空页情况ifnotpage_text.strip():page_text【该页无有效文本内容】# 按页码结构化存储all_page_text.append(f## 第{page_num}页 \n{page_text}\n\n)print(f第{page_num}页提取完成)# 4. 拼接所有页面内容full_text# PDF全量提取结果\n.join(all_page_text)# 5. 保存到文件withopen(output_path,w,encodingutf-8)asf:f.write(full_text)print(f\n全量提取完成结果已保存至{output_path})# 三、精准区域文本提取pdf_file_path3C:\\Users\\hp\\Desktop\\NLP学习数据\\arctle2.pdftarget_page_num1# 目标提取页码从1开始# 提取区域坐标x0: 左边界, y0: 上边界, x1: 右边界, y1: 下边界# 可通过extract_words()查看所有文本块的坐标调整此参数extract_bbox(50,100,500,700)defextract_text_by_area(pdf_path,page_num,bbox): 提取PDF指定页面、指定区域的文本内容 :param pdf_path: PDF文件路径 :param page_num: 目标页码从1开始 :param bbox: 提取区域坐标 (x0, y0, x1, y1) :return: 提取的区域文本 withpdfplumber.open(pdf_path)aspdf:# 转换为0索引page_indexpage_num-1ifpage_index0orpage_indexlen(pdf.pages):print(错误页码超出PDF范围)returnNonetarget_pagepdf.pages[page_index]# 方法1直接通过bbox参数提取指定区域文本推荐area_texttarget_page.extract_text(x_tolerance50,y_tolerance50,keep_blank_charsFalse,bboxbbox)# 方法2先获取所有文本块再筛选区域内的文本更灵活all_wordstarget_page.extract_words()filtered_words[wordforwordinall_wordsifbbox[0]word[x0]andword[x1]bbox[2]andbbox[1]word[y0]andword[y1]bbox[3]]filtered_text .join([word[text]forwordinfiltered_words])# 打印结果print(f 第{page_num}页 指定区域提取结果 )print(直接提取结果)print(area_text)print(\n文本块筛选结果)print(filtered_text)print()print(all_words)returnarea_text# 四、 加密 PDF 文本提取pdf_file_path4C:\\Users\\hp\\Desktop\\NLP学习数据\\arctle4.pdf# 替换为您的加密PDF文件路径pdf_password123456# 替换为PDF的打开密码defextract_encrypted_pdf(pdf_path,pdf_password): 提取加密PDF的文本内容 :param pdf_path: 加密PDF文件路径 :param password: PDF打开密码 try:# 打开加密PDF传入password参数withpdfplumber.open(pdf_path,passwordpdf_password)aspdf:print(f加密PDF打开成功总页数{len(pdf.pages)})# 提取第一页文本作为测试first_page_textpdf.pages[0].extract_text()print( 加密PDF第一页提取结果 )print(first_page_text[:50])# 打印前500个字符预览exceptExceptionase:print(f提取失败错误信息{e})print(常见原因1. 密码错误2. PDF为扫描件/图片型PDF无有效文本3. PDF加密级别过高)# 执行函数if__name____main__:# 基础单页PDF文本提取# extract_single_page_pdf(pdf_file_path1, output_txt_path1)# 多页PDF全量文本提取# extract_multiple_page_pdfs(pdf_file_path2, output_txt_path2)# 精准区域文本提取extract_text_by_area(pdf_file_path3,target_page_num,extract_bbox)# 加密PDF文本提取# extract_encrypted_pdf(pdf_file_path4, pdf_password)

相关新闻