[Python] Dealing with struck-through and non-struck-through characters using Python on Linux...

Discussão em 'Python' iniciado por Stack, Janeiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for an up to date way of determining whether or not text inside an Excel xlsx workbook cell is struck-through using Python on Linux and Mac.

    I understand that Openpyxl will determine if a cell has the struck-through font-type active, but it will only differentiate if the font-type is set at the cell level not at the text level within the cell, which is why I'm stuck.

    I've also read that I can import xlrd v1.2 though I've not tested this as it's not something I want to do if I can help it given that 1.2 is an old out of support version.

    Is there any way that I'm missing of using Python to differentiate struck-through and non-struck-though text within in excel xlsx workbook cell?

    Code tested so far on Mac:

    main_xlsx = "test_wb.xlsx"
    wb = openpyxl.load_workbook(main_xlsx, rich_text=True)
    ws = wb["Sheet1"]

    for i in range(1, ws.max_row+1):
    my_cell = ws["C"+str(i)]
    if type(my_cell.value) == CellRichText:
    for text in my_cell.value:
    if text.font.stike:
    print(text)
    else:
    print("no rich text")

    ------------------------------

    Traceback (most recent call last):
    File "test_main.py", line 17, in <module>
    if text.font.stike:
    ^^^^^^^^^
    AttributeError: 'str' object has no attribute 'font'. Did you mean: 'count'?

    Continue reading...

Compartilhe esta Página