【Python】读取PDF和Word文档

1.Python读取PDF文件

安装处理PDF模块

# 安装PyPDF2模块
pip install PyPDF2

提取PDF文本内容

import PyPDF2
pdfFileObj = open('meetingminutes.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pdfReader.numPages
>>>19
pageObj = pdfReader.getPage(0)
pageObj = extractText()
>>> 'adfadflad\n'

2.Python处理Word文档

安装python-docx模块

pip install python-docx

读取Word文档内容

python-docx中,使用了3中类型来表示文档的结构:

  • Document:表示整个文档;
  • Paragraph:表示文档中的段落;
  • Run:一个Run对象是相同样式文本的延续;
import docx
doc = docx.Document('demo.docx')
len(doc.paragraphs)
>>> 7
doc.paragraphs[1].text
'aaaaaa bbbbbb ccccc'
len(doc.paragraphs[1].runs[0].text)
'aaaaaaa'

docx中获取完整的文本

#! python3
import docx

def getText(filename):
	doc = docx.Document(filename)
	fullText = []
	for para in doc.paragraphs:
		fullText.append(para.text)
	return '\n'.join(fullText)

Reference:Python编程快速上手

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值