实现目标:
使用python批量读取Word简历中的人员信息写入Excel文档
word路径:“D:\学习资料\Python\人员招聘”
实现思路:
1.新建用于存储人员基本信息的Excel文档
2.批量读取Word简历中的信息
3.将信息写入汇总表中
使用的模块:
os :用于获取word文件
docx :用于操作docx文件
openpyxl :用于操作excel文件
实现步骤:
1、新建用于存储人员基本信息的Excel文档。
文档名称为汇总.xlsx。
该文档中含有两个工作表,分别是人员信息登记表,用于存储word数据,工作表的表头有人员的基本信息。
最后保存excel文档,代码如下:
import openpyxl
# 创建一个新工作簿并赋值给变量 wb
wb = openpyxl.Workbook()
# 将默认工作表赋值给asheet变量
sheet= wb["Sheet"]
# 将工作表名称修改为 教师信息登记表
sheet.title = "人员信息登记表"
print(wb.sheetnames)
# 给人员信息登记表、面试人员名单工作表按照预期格式设置表头
sheet["A1"].value = "姓名"
sheet["B1"].value = "性别"
sheet["C1"].value = "出生年月"
sheet["D1"].value = "本科学校"
sheet["E1"].value = "研究生学校"
sheet["F1"].value = "博士学校"
sheet["G1"].value = "最高学历"
sheet["H1"].value = "工龄"
sheet["I1"].value = "应聘岗位"
sheet["K1"].value = "邮箱"
# 保存Excel文档
wb.save("D:\学习资料\Python\人员招聘\汇总.xlsx")
# 输出“汇总Excel创建完成”
print("汇总Excel创建完成")
执行结果如图:
2、批量读取Word简历中的基本信息
我们首先完成批量读Word文档步骤:
1. 导入需要用到的Python办公模块;
2. 获取"D:\学习资料\Python\人员招聘"路径下所有的文件名称;
3. 获得文件夹下所有的文件后,利用for循环去逐个读取文件列表中的文件;
4. 为了避免误读其他类型的文件,用文件后缀名去判断,如果后缀名不是 .docx 就跳过去读下一个文档。
5. 读取文档,再用 .tables属性读取文档中的第一个表格;
6. 使用cell()函数和 .text属性读取教师基本信息。
简历的表格式如下,读Word文档的行和列时索引值。
例如:读姓名内容时 name = table.cell(0, 1).text
代码如下:
# 使用import导入os模块
import os
# 使用import导入docx
import docx
# 获取文件存储路径"D:\学习资料\Python\人员招聘",将路径赋值给path
path = "D:\学习资料\Python\人员招聘"
# 使用os.listdir()函数获取该路径下的所有文件并赋值给fileNames
fileNames = os.listdir(path)
# for循环遍历列表中的所有文件
for fileName in fileNames:
# 使用os.path.splitext()函数获取文件后缀名,赋值给extension
extension = os.path.split(fileName)[1]
# 判断后缀名不是 .docx 时,就跳过
if extension != ".docx":
continue
# 使用os.path.join()函数拼接该文件的路径并赋值给filePath
filePath = os.path.join(path,fileName)
# 使用docx.Document()打开Word文档,赋值给docFile
docFile = docx.Document(filePath)
# 使用.tables获取文档的第一个表格,并赋值给table
table = docFile.tables[0]
# 循环读取教师基本信息
# 读取文档中第一行第二列教师的姓名信息,并赋值name
name = table.cell(0, 1).text
# 读取文档中的其他信息
gender = table.cell(0, 3).text
birthDate = table.cell(0, 5).text
highSchool = table.cell(3, 1).text
masterSchool = table.cell(4, 1).text
doctorSchool = table.cell(5, 1).text
bestGraduate = table.cell(5, 5).text
teachTime = table.cell(2, 1).text
phoneNumber = table.cell(6, 1).text
email = table.cell(6, 5).text
job = table.cell(2, 5).text
3、将信息写入汇总表中
1. 打开存储教师信息的Excel文档 "D:\学习资料\Python\人员招聘\汇总.xlsx" ;
2. 从第2行开始逐个写入(第1行是表头),设置一个计数器,用于标记写入位置;
3. 最后,将Excel文档保存到原路径"D:\学习资料\Python\人员招聘\汇总.xlsx",这样就完成了word表格数据的提取 。
合并后的代码如下:
# 使用import导入os模块
import os
# 使用import导入docx
import docx
# 使用import导入openpyxl模块
import openpyxl
# 创建一个新工作簿并赋值给变量 wb
wb = openpyxl.Workbook()
# 将默认工作表赋值给asheet变量
sheet = wb["Sheet"]
# 将工作表名称修改为 教师信息登记表
sheet.title = "人员信息登记表"
# 给人员信息登记表、面试人员名单工作表按照预期格式设置表头
sheet["A1"].value = "姓名"
sheet["B1"].value = "性别"
sheet["C1"].value = "出生年月"
sheet["D1"].value = "本科学校"
sheet["E1"].value = "研究生学校"
sheet["F1"].value = "博士学校"
sheet["G1"].value = "最高学历"
sheet["H1"].value = "工龄"
sheet["I1"].value = "应聘岗位"
sheet["J1"].value = "手机号"
sheet["K1"].value = "邮箱"
# 保存Excel文档
wb.save("D:\学习资料\Python\人员招聘\汇总.xlsx")
# 获取文件存储路径"D:\学习资料\Python\人员招聘",将路径赋值给path
path = "D:\学习资料\Python\人员招聘"
# 读取Excel文档
wb = openpyxl.load_workbook("D:\学习资料\Python\人员招聘\汇总.xlsx")
asheet = wb["人员信息登记表"]
# 使用os.listdir()函数获取该路径下的所有文件并赋值给fileNames
fileNames = os.listdir(path)
# 设置一个用于记录写入行数的count,初始值为2,从Excel的第二行写入
count = 2
# for循环遍历列表中的所有文件
for fileName in fileNames:
# 使用os.path.splitext()函数获取文件后缀名,赋值给extension
extension = os.path.splitext(fileName)[1]
# 判断后缀名不是 .docx 时,就跳过
if extension != ".docx":
continue
# 使用os.path.join()函数拼接该文件的路径并赋值给filePath
filePath = os.path.join(path,fileName)
# 使用docx.Document()打开Word文档,赋值给docFile
docFile = docx.Document(filePath)
# 使用.tables获取文档的第一个表格,并赋值给table
table = docFile.tables[0]
# 循环读取教师基本信息
# 读取文档中第一行第二列教师的姓名信息,并赋值name
name = table.cell(0, 1).text
# 读取文档中的其他信息
gender = table.cell(0, 3).text
birthDate = table.cell(0, 5).text
highSchool = table.cell(3, 1).text
masterSchool = table.cell(4, 1).text
doctorSchool = table.cell(5, 1).text
bestGraduate = table.cell(5, 5).text
teachTime = table.cell(2, 1).text
phoneNumber = table.cell(6, 1).text
email = table.cell(6, 5).text
job = table.cell(2, 5).text
# 将读到的信息逐行写入单元格
# 将名字赋值给Excel表中A列对应值
asheet[f"A{count}"].value = name
# 将其他信息对应写入Excel文档中
asheet[f"B{count}"].value = gender
asheet[f"C{count}"].value = birthDate
asheet[f"D{count}"].value = highSchool
asheet[f"E{count}"].value = masterSchool
asheet[f"F{count}"].value = doctorSchool
asheet[f"G{count}"].value = bestGraduate
asheet[f"H{count}"].value = teachTime
asheet[f"I{count}"].value = job
asheet[f"J{count}"].value = phoneNumber
asheet[f"K{count}"].value = email
# 写完一个count计数加1,写下一行
count += 1
# 保存Excel文档
wb.save("D:\学习资料\Python\人员招聘\汇总.xlsx")
执行结果如下: