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

Search a word in docx file and copy the file in keyword folder

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

例如 A 具有 C# 技能和 Angular 所以 his/her 简历将在两个文件夹中。

我已完成文件夹创建,我需要帮助来搜索 .docx 文件中的单词并复制到请求的文件夹。我已经在网上寻找资料但无法继续。 谁能告诉我如何在文档中搜索 word/string。文件

这是我的代码:

文件夹创建

 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)

希望对您有所帮助:

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)