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编程快速上手