I am getting none with font name in some files

39 Views Asked by At

I don't know why it tells me None. I have used Python docx library for doing this, I have tried multiple times but didn't find any satisfactory solution.

# Function to extract information about 'Abstract heading Font Style' from the document
def abstract_heading_font_style(word_file):
    doc = Document(word_file)
    font_styles = set() 
    
    for paragraph in doc.paragraphs:
        if any(word in paragraph.text for word in ['Abstract', 'ABSTRACT', 'abstract']):
            for run in paragraph.runs:      
                font_styles.add(run.font.name)
                if any(word in paragraph.text for word in ['Abstract', 'ABSTRACT', 'abstract'])and run.bold:      
                        font_styles.add(run.font.name)
            
    return list(font_styles)
1

There are 1 best solutions below

0
Aditya Malviya On

You're adding the font style to font_styles only if run.bold is True within the nested loop so if there are no bold runs in the paragraph containing 'Abstract' it will always return None since you're not returning anything outside the loop try using below code

from docx import Document


def abstract_heading_font_style(word_file):
    doc = Document(word_file)
    font_styles = set() 
    
    for paragraph in doc.paragraphs:
        if any(word in paragraph.text for word in ['Abstract', 'ABSTRACT', 'abstract']):
            for run in paragraph.runs:
                font_styles.add(run.font.name)
    
    return list(font_styles)

#usage:
word_file = "your_word_file.docx"
abstract_fonts = abstract_heading_font_style(word_file)
print(abstract_fonts)