主頁 > 知識庫 > 使用python處理一萬份word表格簡歷操作

使用python處理一萬份word表格簡歷操作

熱門標(biāo)簽:客戶服務(wù) 百度AI接口 Win7旗艦版 電話運營中心 語音系統(tǒng) 企業(yè)做大做強 硅谷的囚徒呼叫中心 呼叫中心市場需求

前言

有一天朋友A向我抱怨,他的老板要求他把幾百份word填好的word表格簡歷信息整理到excel中,看著他一個個將姓名,年齡……從word表格里復(fù)制粘貼到excel里,邊粘貼心里邊暗暗詛咒著自己的boss……但畢竟新手小白,又不能違背老板的意愿說我不干了,愛咋咋地,于是過來向我求助。我說,這事情好辦啊,學(xué)學(xué)python就能解決啊,簡單容易上手。好了,接下來進入正題。

思路:首先針對每一份word表格進行分析

怎么才能利用python獲取到word表格里面的信息,最初的想法是把word里面的表格轉(zhuǎn)成網(wǎng)頁格式,畢竟混跡爬蟲淺水區(qū)多年,用正則表達式處理網(wǎng)頁來獲取信息是比較輕松的,于是想到把word轉(zhuǎn)成網(wǎng)頁格式,這么一想,整個人都瘋了,幾百份文件打開然后轉(zhuǎn)成網(wǎng)頁,那也有不少勞動量啊。于是在網(wǎng)上搜了許久,發(fā)現(xiàn)docx文件自己本身是壓縮文件,打開壓縮包之后竟然發(fā)現(xiàn)里面有個專門存儲word里面文本的文件。

打開文件找,發(fā)現(xiàn)我們想要的信息全都藏在這個名為document.xml的文件里

于是基本過程就可以確定了

1. 打開docx的壓縮包

2. 獲取word里面的正文信息

3. 利用正則表達式匹配出我們想要的信息

4. 將信息存儲到txt中(txt可以用excel打開)

5. 批量調(diào)用上述過程,完成一萬份簡歷的提取工作

6. (檢查數(shù)據(jù)是否有錯誤或缺失)

0x01 獲取docx信息

利用python的zipfile庫以及re庫來處理docx壓縮包里面的document.xml文件里的信息。

import zipfile
import re
def get_document(filepath):
  z = zipfile.ZipFile(filepath, "r")
  text = z.read("word/document.xml").decode("UTF-8")
  text = re.sub(r".*?>", "", text)#去除xml里的所有標(biāo)記符
  ###如果多份簡歷在同一個word文件里###
  #table_list = text.split("XX簡歷")[1:]#依據(jù)簡歷標(biāo)題切分每一份簡歷信息
  #return table_list
  return text

打印text的結(jié)果

自此,輸出了簡歷中的所有相關(guān)信息

0x02 抓取各字段值

接下來根據(jù)這些相關(guān)信息抓取各個字段的值

import re
def get_field_value(text):
  value_list = []
  m = re.findall(r"姓 名(.*?)性  別", table)
  value_list.append(m)
  m = re.findall(r"性  別(.*?)學(xué)  歷", table)
  value_list.append(m)
  m = re.findall(r"民 族(.*?)健康狀況", table)
  value_list.append(m)  
  '''
  此處省略其他字段匹配
  '''
  return value_list

這樣就將每個字段匹配到的內(nèi)容以一個列表的形式返回了

0x03 將內(nèi)容寫入到文件

接下來將這個列表里的內(nèi)容寫入到txt中

str1 = ""
for value in value_list:
  str1 = str1 + str(value[0]) + "\t"#每個字段值用制表符\t分隔
str1 = str1 + "\n"
with open("result.txt", "a+") as f:#將內(nèi)容以追加形式寫入到result.txt中
  f.write(str1)

以上是將一個word轉(zhuǎn)成了txt

只要再對文件夾中的文件進行批量處理就ok了

0x04 批量處理完整代碼

以下附上完整代碼

import re
import zipfile
import os
def get_document(filepath):
  z = zipfile.ZipFile(filepath, "r")
  text = z.read("word/document.xml").decode("UTF-8")
  text = re.sub(r".*?>", "", text)#去除xml里的所有標(biāo)記符
  ###如果多份簡歷在同一個word文件里###
  table_list = text.split("XX簡歷")[1:]#依據(jù)簡歷標(biāo)題切分每一份簡歷信息
  return table_list
def get_field_value(text):
  value_list = []
  m = re.findall(r"姓 名(.*?)性  別", table)
  value_list.append(m)
  m = re.findall(r"性  別(.*?)學(xué)  歷", table)
  value_list.append(m)
  m = re.findall(r"民 族(.*?)健康狀況", table)
  value_list.append(m)  
  '''
  此處省略其他字段匹配
  '''
  return value_list
cv_list = []
for i in os.listdir(os.getcwd()):
  a = os.path.splitext(os.getcwd() + "\\" + i)#獲取當(dāng)前目錄下所有文件的文件名
  if a[1] == '.docx':#如果文件后綴
    print(os.getcwd()+"\\"+i)
    cv_list = cv_list + get_document(os.getcwd() + "\\" + i)#每份簡歷信息為一個列表元素
for i in cv_list:
  value_list = get_field_value(i)
  str1 = ""
  for value in value_list:
    str1 = str1 + str(value[0]) + "\t"
  str1 = str1 + "\n"
  with open("result.txt", "a+") as f:
    f.write(str1)

一萬份word表格簡歷信息轉(zhuǎn)成了txt,然后用excel打開txt即可。

補充:python word表格一些操作

數(shù)據(jù)格式(datas): 列表套列表

aa =[ [1,2,3,4,5],[6,7,8,9],[]…]
import os
import requests
import json
import datetime
from docx import Document
from docx.shared import Inches, Pt, Cm
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
def create_insert_word_table(datas, stday, etday, s):
  """創(chuàng)建word表格以及插入數(shù)據(jù)"""
  doc = Document()
  doc.styles['Normal'].font.name = 'Calibri' # 是用來設(shè)置當(dāng)文字是西文時的字體,
  doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體') # 是用來設(shè)置當(dāng)文字是中文時的字體
  # doc.styles['Normal'].font.size = Pt(14) # 設(shè)置所有文字字體大小為14
  distance = Inches(0.5)
  sec = doc.sections[0] # sections對應(yīng)文檔中的“節(jié)”
  sec.left_margin = distance # 以下依次設(shè)置左、右、上、下頁面邊距
  sec.right_margin = distance
  sec.top_margin = distance
  sec.bottom_margin = distance
  sec.page_width = Inches(11.7) # 設(shè)置頁面寬度
  # sec.page_height = Inches(9) # 設(shè)置頁面高度
  # doc.add_heading() # 設(shè)置標(biāo)題,但是不符合我的條件,只能試用下方p.add_run('我是文字')
  p = doc.add_paragraph() # 添加段落
  p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 設(shè)置中央對齊
  run = p.add_run('我是文字')
  run.font.size = Pt(22)
  doc.add_paragraph() # 添加空段落
  # 添加表格
  table = doc.add_table(rows=1, cols=10, style='Table Grid')
  table.style.name = 'Table Grid'
  table.style.font.size = Pt(14)
  table.rows[0].height = Cm(20)
  title = table.rows[0].cells
  title[0].text = '姓名'
  title[1].text = '1'
  title[2].text = '2'
  title[3].text = '3'
  title[4].text = '4'
  title[5].text = '5'
  title[6].text = '6 '
  title[7].text = '7'
  title[8].text = '8'
  title[9].text = '9'
  for i in range(len(datas)):
    cels = table.add_row().cells
    for j in range(len(datas[i])):
      # cels[j].text = str(datas[i][j])
      p = cels[j].paragraphs[0]
      p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 設(shè)置中央對齊
      p.add_run(str(datas[i][j]))
      ph_format = p.paragraph_format
      # ph_format.space_before = Pt(10) # 設(shè)置段前間距
      # ph_format.space_after = Pt(12) # 設(shè)置段后間距
      ph_format.line_spacing = Pt(40) # 設(shè)置行間距
  doc.save('./files/項目總結(jié).docx')

生成示例

可能出現(xiàn)的錯誤,[Errno 13] Permission denied: ‘./files/項目進展總結(jié).docx'

是因為你打開文件未關(guān)閉,操作不了,關(guān)閉他就好了

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 使用Python 統(tǒng)計文件夾內(nèi)所有pdf頁數(shù)的小工具
  • 20行Python代碼實現(xiàn)一款永久免費PDF編輯工具的實現(xiàn)
  • 用python 制作圖片轉(zhuǎn)pdf工具
  • Python開發(fā)的單詞頻率統(tǒng)計工具wordsworth使用方法
  • Python快速優(yōu)雅的批量修改Word文檔樣式
  • python提取word文件中的所有圖片
  • 教你如何利用Python批量翻譯英文Word文檔并保留格式
  • 詳解用Python把PDF轉(zhuǎn)為Word方法總結(jié)
  • python 三種方法提取pdf中的圖片
  • 只用40行Python代碼就能寫出pdf轉(zhuǎn)word小工具

標(biāo)簽:山西 安康 濟南 崇左 長沙 海南 山西 喀什

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用python處理一萬份word表格簡歷操作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266