A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

当你想批量提取文档(如简历)中的电话和邮箱,可以参考以下代码:

提取结果保存在“resumes.xlsx”表格中。

import os

from win32com import client as wc

import glob

from shutil import copyfile

import os.path,re

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter

from pdfminer.converter import PDFPageAggregator

from pdfminer.layout import LAParams

from pdfminer.pdfpage import PDFTextExtractionNotAllowed,PDFPage

from pdfminer.pdfparser import PDFParser

from pdfminer.pdfdocument import PDFDocument

from openpyxl import Workbook

'''

step 1:

将doc、docx格式的简历转换为 pdf 文件后复制到 pdfPath 文件夹下,

将pdf格式的简历直接复制到 pdfPath 文件夹下,

'''

word = wc.Dispatch('Word.Application')

print('当前工作路径:' + os.getcwd())

# 处理路径

FolderPath = os.getcwd() # 脚本工作路径

SaveFolderPath = FolderPath + '\\pdfPath' # pdf 格式简历保存路径

os.mkdir(SaveFolderPath) # 创建文件夹

WordPath = FolderPath + '/*[doc, docx]' # 筛选出doc和docx格式的文件

PdfPath = FolderPath + '/*[pdf]' # 筛选出pdf格式的文件

print('\n简历格式转换处理中...\n')

# 将当前目录下的 doc,docx 文件转换成 pdf 文件后,放到pdfPath文件夹

files = glob.glob(WordPath)

for file_path_word in files:

# 获取文件名

name = os.path.basename(file_path_word)

names = re.findall(r'(.*?).doc', name)[0]

print(names + '.pdf')

doc = word.Documents.Open(file_path_word)

doc.SaveAs(SaveFolderPath + '\\%s.pdf'%names, 17)

doc.Close()

# 将当前目录下的 pdf 文件拷贝到 pdfPath 文件夹

files = glob.glob(PdfPath)

for file_path_pdf in files:

name = os.path.basename(file_path_pdf)

names = re.findall(r'(.*?).pdf',name)[0]

print(names + '.pdf')

copyfile(file_path_pdf, SaveFolderPath + '\\%s.pdf'%names)

word.Quit()

'''

step 2:

解析pdf文件

'''

class CPdf2TxtManager():

def changePdfToText(self, filePath):

getInfo = {'Phone': None, 'Email': None}

# 以二进制读模式打开

file = open(filePath, 'rb')

# 用文件对象来创建一个pdf文档分析器
外汇监管http://www.fx61.com/supervision

praser = PDFParser(file)

# 创建一个PDF文档对象存储文档结构,提供密码初始化,没有就不用传该参数

doc = PDFDocument(praser, password='')

# 检查文件是否允许文本提取

if not doc.is_extractable:

raise PDFTextExtractionNotAllowed

# 创建PDf 资源管理器 来管理共享资源,#caching = False不缓存

rsrcmgr = PDFResourceManager(caching = False)

# 创建一个PDF设备对象

laparams = LAParams()

# 创建一个PDF页面聚合对象

device = PDFPageAggregator(rsrcmgr, laparams=laparams)

# 创建一个PDF解析器对象

interpreter = PDFPageInterpreter(rsrcmgr, device)

# 获得文档的目录(纲要),文档没有纲要会报错

# PDF文档没有目录时会报:raise PDFNoOutlines pdfminer.pdfdocument.PDFNoOutlines

# print(doc.get_outlines())

# 获取page列表

#print(PDFPage.get_pages(doc))

# 循环遍历列表,每次处理一个page的内容

for page in PDFPage.create_pages(doc):

interpreter.process_page(page)

# 接受该页面的LTPage对象

layout = device.get_result()

# 这里layout是一个LTPage对象 里面存放着 这个page解析出的各种对象

# 一般包括LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等等

for x in layout:

if hasattr(x, 'get_text'):

fileNames = os.path.splitext(filePath)

results = x.get_text()

# print('###' + results)

# 匹配邮箱

emailRegex = re.compile(r'''(

[a-zA-Z0-9._%+-]+ # 邮箱用户名

@ # @ symbol

[a-zA-Z0-9.-]+ # 域名

(.[a-zA-Z]{2,4}) # 域名后缀

)''', re.VERBOSE)

matchedEmail = emailRegex.search(results)

if matchedEmail:

# print(matchedEmail.group())

getInfo['Email'] = matchedEmail.group()

# 匹配手机号

phoneRegex = re.compile(r'''(

([1]) # 手机号码通常以‘1’开始

(\d{2}) # 紧随其后有两个数字

(\s|-|.|'')? # 可能有分隔符如‘-’ ‘.’ 或空格

(\d{4}) # 四个数字

(\s|-|.|'')? # 可能有分隔符如‘-’ ‘.’ 或空格

(\d{4}) # 四个数字

(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension

)''', re.VERBOSE)

matchedPhone = phoneRegex.search(results)

if matchedPhone:

# print(matchedPhone.group())

phoneNumber = matchedPhone.group()

phoneNumber = phoneNumber.replace(' ', '') # 去除空格

phoneNumber = phoneNumber.replace('-', '') # 去除 '-'

phoneNumber = phoneNumber.replace('.', '') # 去除 '.'

getInfo['Phone'] = phoneNumber

return getInfo

'''

step 3:

保存求职者信息到Excel文件

'''

print('\n简历信息提取...')

dirs = os.listdir(SaveFolderPath) # 搜索目录

pdf2TxtManager = CPdf2TxtManager()

wb = Workbook() # 创建文件对象

ws = wb.active # 获取第一个sheet

# 将数据写入到指定的单元格

ws['A1'] = '姓名'

ws['B1'] = '电话'

ws['C1'] = '邮箱'

# 提取求职者的联系方式,并写入Excel文件对象

i = 2

for file in dirs:

retInfo = pdf2TxtManager.changePdfToText(SaveFolderPath + '\\' + file)

name = re.findall(r'(.*?).pdf',file)[0]

print('\n

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马