Home  >  Article  >  Backend Development  >  How to organize attachments using python

How to organize attachments using python

不言
不言Original
2018-06-04 17:42:501445browse

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

First we standardize the file name, because when using subprocess.call to call commands, spaces, special symbols and the like There is no way to escape and an error will be reported, so we might as well clean up this potential problem beforehand.

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

After that we can get down to business and traverse each file for parsing:

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

The next step is to parse the file, which is easy to achieve through the docx module , I won’t post the specific parsing logic, it just traverses each line and intercepts the data based on some keywords and symbols (the format of each resume is basically the same)

doc = docx.Document(fullname)
for para in doc.paragraphs:
  print(para.text)
  ...具体解析逻辑...

Filling excel

The parsed data can be filled directly in 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

At this point, the corresponding function can basically be realized, but it is not perfect, mainly in word The format in is not standard, and there is no good way to accurately obtain the data I want, but most of the major names, schools, etc. have been captured, which can be regarded as a lighter task.

Summary

Using python still reduces a certain amount of repetitive work, but there seems to be no good way to deal with some non-standard stuff.

Although logic can be added to accommodate these non-standards, it is obvious that the effort and output are somewhat disproportionate.

It is true to make good use of the tools at hand to improve efficiency. As for whether it is fool-like duplication of work, or whether to use code to reduce duplication of work, it depends on how you look at it.

Related recommendations;

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn