Home >Backend Development >Python Tutorial >How to organize attachments using python
This article has compiled relevant knowledge points about how to use python to organize attachments. Friends who are learning python can follow along and test it.
Currently there are more than 500 resumes in my folder. If I want to know some information, such as school, academic qualifications, etc., I need to open each word to view it, which is too time-consuming. At this time python needs to take action.
Goal
There are currently 600 words similar to those in the screenshot, and I want to simply organize them:
You can organize an excel file for navigation (similar to a directory), and you can use excel to quickly locate the attachments you want, as shown below:
Specific implementation
Once we have the goal, let’s talk about how to achieve it in detail. The arrangement of ideas is relatively simple, which is to traverse all word files and obtain the key information in word. and save to excel.
Here are the main modules used:
import xlsxwriter import subprocess import os import docx import sys import re##xlsxwriter is mainly used to operate excel, xlsxwriter can only be used to write, in terms of efficiency It is higher than xlwt, and the amount of data is not much. It is ok to use xlwt. Subprocess is mainly used to call the command line. Because the docx module cannot parse the doc word file, it converts the doc file into a docx file before parsing. os is mainly used to traverse folders to obtain files. docx is mainly used to parse word documents.
Standardize the file name
def remove_doc_special_tag(): for filename in os.listdir(path): otherName = re.sub("[\s+\!\/_,$%^*(+\"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+", "",filename) os.rename(os.path.join(path,filename),os.path.join(path,otherName))
Traversing files
path='/Users/cavin/Desktop/files' for filename in os.listdir(path): ...具体逻辑...I encountered a problem here. First, the docx module cannot parse the doc word document. Since I am using a mac, I cannot use the win32com module. This problem is quite embarrassing. Later, Google discovered that doc can be converted into docx through commands. Note here that the converted docx file style is lost, but this does not affect my ability to obtain text information. So there is this code. If it is a doc file, it will be converted to docx first, and then removed after the parsing is completed.
if filename.endswith('.doc'): subprocess.call('textutil -convert docx {0}'.format(fullname),shell=True) fullname=fullname[:-4]+".docx" sheetModel= etl_word_files(fullname)#解析文本逻辑 subprocess.call('rm {0}'.format(fullname),shell=True) #移除转换的文件
Parse the word file
doc = docx.Document(fullname) for para in doc.paragraphs: print(para.text) ...具体解析逻辑...
Filling excel
workbook = xlsxwriter.Workbook('report_list.xlsx') worksheet = workbook.add_worksheet('list') worksheet.write(0,0, '序号') worksheet.write(0,1, '姓名') worksheet.write(0,2, '性别') worksheet.write(0,3, '年龄') worksheet.write(0,4, '籍贯') worksheet.write(0,5, '目前所在地') worksheet.write(0,6, '学历') worksheet.write(0,7, '学校') worksheet.write(0,8, '公司') worksheet.write(0,9, '职位') worksheet.write(0,10, '文档链接')The main topic here is to fill in the document link. Since it is for other people, just make sure that the attachment and excel are in the same folder and use a relative path to achieve it. You can use the Excel function HYPERLINK:
worksheet.write(index,10, '=HYPERLINK(\"./'+filename+'\",\"附件\")')
Problem point
Summary
Use Python to quickly build HTTP services and file sharing services
Use Python to monitor file content changes code
The above is the detailed content of How to organize attachments using python. For more information, please follow other related articles on the PHP Chinese website!