【Python】批量从doc简历中提取出需要的信息
最近幫公司HR從智聯招聘下載簡歷錄入信息,寫了個小程序自動錄入。
第一步 把doc文件轉為txt文件
因為doc文件中嵌套大量隱藏表格,超鏈接之類的格式,用docx這個庫讀取時很多信息顯示不出來(也可能是我不會),就想到把doc轉換為無格式的txt文件。
第二步 從txt文件中提取信息
轉換為txt后驚喜的發現不同文件的相同信息基本都在相同的位置,比如姓名、性別;
對于不在相同位置的信息,就用正則表達式匹配。
第三步 把上述信息存入excel導出
第四步 把程序打包成exe文件運行
- 參考以下鏈接:https://blog.csdn.net/qq_34686440/article/details/89001114*
雖然程序在jupyter notebook里可以運行,但是,打包好的exe文件總是閃退,這里出現了幾個問題:
我把第二步 “從txt文件中提取信息” 和第三步“導出excel”分成3個函數寫,一個是獲取文件夾中txt文件,返回一個列表;第二個是一個一個打開文件獲取信息;第三個函數是導出excel。這里第二個函數需要用到第一個函數的返回值;第三個函數也需要用到第二個步驟里返回的列表,寫入excel,這里出現了點問題,后來我把這3個函數合并成一個了。
合并函數后還是會閃退(就只執行到把所有doc轉換成txt就閃退了),但是并沒有顯示問題在哪。后來通過cmd里直接運行exe文件,得知了錯誤在哪。
- 參考以下鏈接:https://blog.csdn.net/wltsysterm/article/details/104632879*
顯示如下錯誤:
'utf-8' codec can't decode byte 0xbc in position 0: invalid start byte**原因是我之前轉換的txt文件中是中文,但并沒有指定為utf-8格式,所以在轉換txt的時候,直接指定為utf-8格式的txt。并在打開txt文件提取信息時,指定utf-8格式。
修改了這2處,exe文件終于可以運行了!
- 參考以下鏈接:https://blog.csdn.net/sinat_33455447/article/details/101020285*
完整代碼
import os import fnmatch import win32com.client import xlwt import rePATH_DATA = os.path.abspath(r"C:\Users\Desktop\resume") #word簡歷存放路徑 path = r"C:\Users\Desktop\resume" # 導出txt文件存放路徑def to_txt():wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")try:for root, dirs, files in os.walk(PATH_DATA):for _dir in dirs:passfor _file in files:if not (fnmatch.fnmatch(_file, '*.doc') or fnmatch.fnmatch(_file, '*.docx')) or _file.startswith("~"):continueprint('_file:',_file)file = os.path.join(root, _file)wordapp.Documents.Open(file)if fnmatch.fnmatch(_file, '*.doc'): #匹配doc文檔file = file[:-3] + 'txt'else: #匹配docx文檔file = file[:-4] + 'txt'wordapp.ActiveDocument.SaveAs(file, FileFormat=win32com.client.constants.wdFormatText,Encoding=65001) # 這里直接轉換為 utf-8 格式的txt# https://docs.microsoft.com/zh-cn/office/vba/api/Office.MsoEncoding 各種格式代碼在這里查wordapp.ActiveDocument.Close()finally:wordapp.Quit()# In[25]:def fileinfo():files = os.listdir(path)fileList = []for f in files:if(os.path.isfile(path + '/' + f)): # 判斷是否為文件if f.endswith("txt"):fileList.append(f)list = []for file in fileList:data = []with open(path + "/" + file,'r',encoding='UTF-8') as f: # 這里指定為utf-8for line in f:data.append(line) inlist = []# 獲取姓名,所有文檔姓名位置相同inlist.append(data[10][:-1]) # 獲取性別,所有文檔性別位置相同inlist.append(data[13][0])# 獲取電話號碼t = []for ls in data:y = re.findall("1[0-9]{10}",ls)if len(y) != 0 :t = yinlist.append(t[0]) # 獲取郵箱k = []for ls in data:y = re.findall("E-mail.*@*.*",ls)if len(y) != 0 :k = yinlist.append(k[0][7:])# 獲取學校專業學歷e = []for ls in data:y = re.findall("教育經歷.*",ls)if len(y) != 0 :num = data.index(ls)+1e = data[num]e = e.split()inlist.append(e[-3:][0])inlist.append(e[-3:][1])inlist.append(e[-3:][2])# 獲取時間inlist.append(data[7][:-1])list.append(inlist)# 寫入excelbook = xlwt.Workbook()sheet = book.add_sheet('sheet1')# hang = 0for i in range(len(list)):for j in range(len(list[i])):sheet.write(i, j, list[i][j])book.save(r"C:\Users\Desktop\resume\resume"+'.xls') # excel存放路徑if __name__ == '__main__': to_txt()fileinfo()os.system("pause")總結
以上是生活随笔為你收集整理的【Python】批量从doc简历中提取出需要的信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51单片机控制彩色点阵制作沙漏型交通灯
- 下一篇: 30、基于51单片机交通灯车流量管控数码