在docx文件中搜索单词并将该文件复制到关键字文件夹中

问题描述 投票:1回答:1

我正在做一个任务,我有一个包含特定关键字喜欢的文本文件(C#,Angular,Python,Rest,MySQL),并且必须创建文件夹。在此之后,我必须在给出的简历中搜索这些关键字,如果找到则将其复制到每个文件夹。

例如,A具有C#和Angular的技能,因此他/她的Resume将在两个文件夹中。

我已完成文件夹创建,我需要帮助搜索.docx文件中的单词并复制到请求的文件夹。我找过网上的东西,但无法继续。任何人都可以为我提供一些如何在文档中搜索单词/字符串。文件

这是我的代码:

文件夹创建

 InputFile = "ResumeKeyword.txt"
 fileOpen  = open(InputFile)        

 for keyword in fileOpen.readline().split(','):
     print(keyword)
     os.makedirs(keyword)

 fileOpen.close()

并阅读docx

from docx import Document
document = Document('A.docx')
word = "Angular"
for x in document.paragraphs:
    print(x.text)
python-3.x python-2.7 python-docx
1个回答
1
投票

希望这可以帮助:

from docx import Document
from shutil import copyfile
import os, re, random

# Folder which contains all the resumes
ALL_RESUMES = "all_resumes/"
# The folder which will contain the separated resumes
SEGREGATED_RESUMES = "topic_wise_resumes/"

def get_keywords(keywords_file, create_new = False):
    """
    Get all keywords from file keywords_file. We get all keywords in lower case to remove confusion down the line.
    """
    fileOpen  = open(keywords_file, "r")        
    words = [x.strip().lower() for x in fileOpen.readline().split(',')]

    keywords = []
    for keyword in words:
        keywords.append(keyword)
        if(not(os.path.isdir(SEGREGATED_RESUMES))):
            os.makedirs(SEGREGATED_RESUMES + keyword)

    return keywords

def segregate_resumes(keywords):
    """
    Copy the resumes to the appropriate folders
    """
    # The pattern for regex match
    keyword_pattern = "|".join(keywords)

    # All resumes
    for filename in os.listdir(ALL_RESUMES):
        # basic sanity check
        if filename.endswith(".docx"):
            document = Document(ALL_RESUMES + filename)
            all_texts = []
            for p in document.paragraphs:
                all_texts.append(p.text)

            # The entire text in the resume in lowercase
            all_words_in_resume = " ".join(all_texts).lower()

            # The matching keywords
            matches = re.findall(keyword_pattern, all_words_in_resume)

            # Copy the resume to the keyword folder
            for match in matches:
                copyfile(ALL_RESUMES + filename, SEGREGATED_RESUMES + match + "/" + filename)

def create_sample_resumes(keywords, num = 5):
    """
    Function to create sample resumes for testing
    """
    for i in range(num):
        document = Document()
        document.add_heading('RESUME{}'.format(i))
        skills_ = random.sample(keywords, 2)
        document.add_paragraph("I have skills - {} and {}".format(*skills_))
        document.save(ALL_RESUMES + "resume{}.docx".format(i))

keywords = get_keywords("ResumeKeyword.txt")
print(keywords)
# create_sample_resumes(keywords)
segregate_resumes(keywords)
© www.soinside.com 2019 - 2024. All rights reserved.