Home > front end >  Is there a method to check if a portion of text inside a xlsx cell is bold?
Is there a method to check if a portion of text inside a xlsx cell is bold?

Time:11-09

Using openpyxl we can properly check if a cell is fully bold/not bold, but we cannot work with richtext so having two words, one bolded and one not, will make the check fail.

This can be done correctly with xlrd, but it doesn't support xlsx files. Converting from xlsx to xls is risky, especially in my use case, since I have a big file with many languages and I think i could lose information.

How can I check if cells substrings are bold inside a xlsx file?

CodePudding user response:

TL;DR: not yet, but upcoming openpyxl v3.1 will be able to satisfy your request.


I took a quick tour through the late 2022 state of python-excel affair with respect to this very feature, which relies on being able to manage Rich Text objects as cell contents:

  • pylightxl was new to me, so I quickly browsed the project for a bit. But as the name indicates, it is geared towards a small, maintainable feature set. Only cell values are in scope, formatting is intentionally skipped.

  • xlrd supports rich text, though only legacy .xls, as you pointed out in your question already.

  • xlsxwriter luckily is able to construct cells containing rich text with mixed formatting (enter image description here then use:

    import openpyxl
    test_file = "test_spreadsheet.xlsx"
    
    obj = openpyxl.load_workbook(test_file)
    sheet_ref = obj.active
    
    
    
    cell1 = sheet_ref.cell(row = 3, column = 2)
    cell_font1 = cell1.font
    print(cell_font1.b)
    cell = sheet_ref.cell(row = 4, column = 2)
    cell_font = cell.font
    print(cell_font.b)
    
    

    result:

    $ python test.py
    False
    True
    

    so you could build it yourself:

    def is_text_in_cell_bold(cell_obj):
        if (cell_obj.font.b):
            return True
        return False
    
  • Related