Assign part of a string to list

Question:

I’m trying to extract text from a PDF using PDFminer.six, is there a way to find all instances of a certain phrase appearing in that string. I know a way to find the phrases and remove them but I can’t seem to save the text around the phrase to a variable or list. Is there an easy way to do this that I’ve overlooked?

from pdfminer.high_level import extract_text

text = extract_text('Pdf Scanner/test.pdf')

textf = text.find("vejkode")

print(len(text))

This is what I have so far.
Sorry if this a dumb question, I’m relatively new to programming.

Asked By: SykesTheLord

||

Answers:

def extract_phrase(keyword='vejkode', file='test.pdf', window=30):
    text = extract_text(file)
    start = text.find(keyword)
    end = start + len(keyword)
    phrase = text[start - window:end + window]
    return phrase.split()[1:-1]  # trim truncated words at each end
Answered By: J_H
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.