Reputation: 41
I have thousands of resumes in any format like word with .doc, .docx and pdf.
I want to extract bold text from these documents using textract library in python. is there a way to extract using textract?
Upvotes: 3
Views: 13416
Reputation: 409
An easy solution would be to use the python-docx package. install the package using ( !pip install python-docx )
You'll need to convert your pdf files to .docx . you can do that using any online pdf to docx converter or use python to do that.
the following lines of codes will extract all bold and italic contents of your resumes and save them in a dictionary called boltalic_Dict. you may retrieve either later on.
from docx import *
document = Document('path_to_your_files')
bolds=[]
italics=[]
for para in document.paragraphs:
for run in para.runs:
if run.italic :
italics.append(run.text)
if run.bold :
bolds.append(run.text)
boltalic_Dict={'bold_phrases':bolds,
'italic_phrases':italics}
Upvotes: 5
Reputation: 378
Building on m.borhan's answer, since in their code some contiguous bold and italic portions failed to output as single item:
from docx import *
document = Document('path_to_your_files')
bolds=[]
italics=[]
last_bold = "" #last bold part
last_italic = "" #last italic part
for para in document.paragraphs:
for run in para.runs:
if run.italic :
last_italic = last_italic + run.text
elif run.bold :
last_bold = last_bold + run.text
else:
italics.append(last_italic)
bolds.append(last_bold)
last_italic = ""
last_bold = ""
italics = [i for i in italics if i]
bolds = [i for i in bolds if i]
boltalic_Dict={'bold_phrases':bolds,
'italic_phrases':italics}
Upvotes: 2