当前位置: 移动技术网 > IT编程>脚本编程>Python > 利用python处理简历和名单——处理excel & os、shutil处理文件

利用python处理简历和名单——处理excel & os、shutil处理文件

2020年07月14日  | 移动技术网IT编程  | 我要评论

处理excel

https://blog.csdn.net/sinat_28576553/article/details/81275650

实操例子:

背景:两张excel表,一张里面是名单与安排好的职位与时间,另一张里面是每个人的详细信息,现在想将第二张表中的详细信息加到第一张表中。
名单(2)是第一张表
最终名单是第二张表

import xlrd,xlwt
#打开两个excel文件
data1=xlrd.open_workbook(r"F:\文件\其他\志愿者\终面\名单 (2).xlsx")
data2=xlrd.open_workbook(r"F:\文件\其他\志愿者\终面\最终名单.xlsx")
#打开其中的第一个表,并用名单中的姓名栏作为字典的键,其余项作为对应的值(我的名单中第一列是姓名)
sheet1=data1.sheet_by_index(0)
dict_1={sheet1.row_values(i)[0]:sheet1.row_values(i)[1:] for i in range(sheet1.nrows)}
sheet2=data2.sheet_by_index(0)
dict_2={sheet2.row_values(i)[0]:sheet2.row_values(i)[1:] for i in range(sheet2.nrows)}

#将两个excel合并,合并前对电话号码和QQ号进行了字符串化(名单中第二列和第三列是电话号码和QQ号,第一行是目录,所以continue跳过)
for key in dict_2.keys():
    if key=="您的名字":
        continue
    for i in range(2):
        try:
            dict_2[key][i]=str(int(dict_2[key][i]))
        except ValueError:
            print("w")#大概看一下有什么错,但不想程序停下来
            pass
for i in dict_1.keys():
    if i in dict_2.keys():
        dict_1[i]=dict_1[i]+dict_2[i]#合并
print(dict_1)
#写入新的excel文件
list_res=[([i]+dict_1[i])for i in dict_1.keys()]#把字典拆开为列表
print(list_res)
book=xlwt.Workbook(encoding="utf-8",style_compression=0)
sheet=book.add_sheet("name",cell_overwrite_ok=True)
for i in range(len(list_res)):
    for j in range(0,len(list_res[i])):
        sheet.write(i,j,list_res[i][j])
book.save(r"F:\文件\其他\志愿者\终面\name.xls")#保存

这里选择了使用稍微落后的xlrd,xlwt来操作
在引文中后面提到,如今的openpyxl会更好,但是因为他的输出是个元组,读值是还需要用“.value”,所以我本来是按照前面的xlrd,xlwt写的,也就懒得改了,因此在程序运行完后还要去把表格另存为xlsx(xlrd,xlwt生成的是比较老的xlx)。

利用os和shutil进行文件复制重命名等操作

https://blog.csdn.net/u012005313/article/details/49277059
https://blog.csdn.net/weixin_41010198/article/details/103307561

实操:
背景:
test1.txt文件中是一串名单,每行一个名字。这个名单是筛选一遍后留下的人,现在需要把这些人的简历从所有人的简历中找出来放在一个文件夹中进行第二遍筛选。
重命名是因为想要把简历

import os
import shutil
path_name = r'F:\文件\其他\志愿者\报名 筛选\test1.txt' #需要简历的人的名单文件路径
path_doc = r'F:\文件\其他\志愿者\报名 筛选\智博会' #所有简历的文件路径
copy_to = r'F:\文件\其他\志愿者\终面\筛选' #拷贝保存路径
doclist=[]
cou=2

isExists = os.path.exists(copy_to)#如果拷贝路径不存在就新建一个文件夹
if not isExists:
    os.makedirs(copy_to)

with open(path_name,"r",encoding="utf8")as f:#读取名单,放入列表
    name = []
    while True:
        name1 = f.readfline().strip()
        if name1 == "": break
        name.append(name1)

for i in doclist:
    copy_to=r'F:\文件\其他\志愿者 智博会\终面\筛选'#初始化
    i=i+".doc"#加上后缀名
    docfilepath = os.path.join(path_doc, i)#合成文件路径

    copy_to_change_name=str(cou)+i#给文件名加上序号(重命名)
    copy_to=os.path.join(copy_to,copy_to_change_name)#合成拷贝路径
    # try:
    shutil.copy(docfilepath, copy_to)#拷贝并重命名
    cou+=1
    # except(FileNotFoundError):
    #     print(FileNotFoundError)

本文地址:https://blog.csdn.net/weixin_45941945/article/details/107310007

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网