使用Python解析pdf、docx等格式文件。

针对不同类型的文件,需要采取特定的访问与解析策略来有效获取其中蕴含的知识。下面我们将介绍对于不同数据源数据的获取方式。

1 解析Docx文档

1.1 获取Docx文档中文本

python 复制代码
from docx import Document
# pip install python-docx
# python-docx == 0.8.11
filename = 'xxx.docx'
doc = Document(filename)
for para in doc.paragraphs:
    print(para.text)

1.2 获取Docx文档中表格

python 复制代码
from docx import Document
filename = r'sample.docx'
doc = Document(filename)
print(f"\n 便利文档中表格:")
print(f"\n 方法一:")
for table in doc.tables:
    row_count = len(table.rows)
    col_count = len(table.columns)
    for i in range(row_count):
        row = table.rows[i].cells
        print(f"row : {row}")
print(f"\n 方法二:")
for table in doc.tables:
    row_count = len(table.rows)
    col_count = len(table.columns)
    for i in range(row_count):
        for j in range(col_count):
            print(table.cell(i,j).text)

2 解析txt文件

2.1 读取富文本txt

  • read() ------ 读取所有文本
python 复制代码
with open('sample.txt','r+',encoding='utf-8') as f:
	data = f.read()
	print(data)
  • readline() ------ 读取第一行的内容
python 复制代码
with open('sample.txt','r+',encoding='utf-8') as f:
	data = f.readline()
	print(data)
  • readlines() ------ 读取全部内容,以数列的形式返回结果
python 复制代码
with open('sample.txt','r+',encoding='utf-8') as f:
	data = f.readlines()
	print(data)
python 复制代码
with open('sample.txt','r+',encoding='utf-8') as f:
	for ann in f.readlines()
	ann = ann.strip('\n') # 去除文本中的换行符
	print(ann)
# 简单版本
for i in open(file='sample.txt',encoding='utf-8').readlines():
    ann = i.strip('\n')
    print(ann)

3 解析PDF

3.1 PDF解析神器------pdfplumber

3.1.1 安装
python 复制代码
pip install pdfplumber
3.1.2 提取pdf中的纯文本
python 复制代码
import pdfplumber
file_name = r'sample.pdf' # 需要解析的pdf文件
output_file = 'sample.txt' # pdf解析后的内容
with pdfplumber.open(file_name) as p:
    page_count = len(p.pages)
    for i in range(0,page_count):
        page = p.pages[i]
        text_data = page.extract_text()
        data = open(output_file,'a',encoding='utf-8')
        data.write(text_data)
3.1.3 提取pdf中的表格
python 复制代码
import pdfplumber
from openpyxl import Workbook # 保存表格
file_name = r'sample.pdf'
output_file = 'sample.xlsx'
with pdfplumber.open(file_name) as pdf:
    page = pdf.pages[0]
    table = page.extract_table()
    workbook = Workbook()
    sheet = workbook.active
    for row in table:
        sheet.append(row)
    workbook.save(filename=output_file)
  • extract_tables()方法------输出文档所有表格,返回一个嵌套列表。
python 复制代码
#extract_tables()法
with pdfplumber.open(r'exm.pdf') as pdf:  # 打开pdf
    page_one = pdf.pages[0]  
    page_one_table =page_one.extract_tables()  # 获取pdf第一页的所有表格数据
    for row in page_one_table:
       print('第一页的表格数据:', row)
  • extact_table()方法------不会返回文档的所有表格,仅返回行数最多的表格数据。如存在多个行数相等的表格,则默认输出顶部表格数据。表格的每一行都为一个单独的列表,列表中的元素即为原表格的各个单元格的数据。
python 复制代码
# extract_table()法
with pdfplumber.open(r'exm.pdf') as pdf_info:  # 打开pdf
    page_one = pdf_info.pages[0]  
    page_one_table = page_one.extract_table()
    for row in page_one_table:
        print(row)
复制代码
相关推荐
码出钞能力34 分钟前
linux内核模块的查看
linux·运维·服务器
星辰云-1 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
聽雨2371 小时前
02每日简报20250704
linux·科技·金融·生活·社交电子·娱乐·媒体
Maki Winster2 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
巴里巴气2 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19892 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金3 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen3 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm
前端付豪3 小时前
20、用 Python + API 打造终端天气预报工具(支持城市查询、天气图标、美化输出🧊
后端·python
前端付豪3 小时前
19、用 Python + OpenAI 构建一个命令行 AI 问答助手
后端·python