-1
import openpyxl as xl
from openpyxl.styles import Font, colors

wb = xl.load_workbook('schedule.xlsx')
sheet = wb['Schedule']

for row in range(2, sheet.max_row + 1):
    for column in range(1, 7):
        cell = sheet.cell(column, row)
        word = str(cell.value)

        if '-' in word:
            index = word.index('-')
            part = word[index:]
            print(part)

            # ???????
            cell.font = Font(color=colors.RED)

wb.save('new_schedule.xlsx')

Hello, I want this program to pick cells that contain '-'. Then it should colour only the part after '-' with red colour. Code I made makes whole cell red, I haven't found anything useful in documentation of this package.

Would really appreciate some suggestions

  • Possible duplicate of [Editing workbooks with rich text in openpyxl](https://stackoverflow.com/questions/28774757/editing-workbooks-with-rich-text-in-openpyxl) – Charlie Clark Oct 07 '19 at 08:41

1 Answers1

0

I looked through the documetation, didn't find anything useful. After looking here I found these two: This user tried doing the same thing openpyxl change one word's color in the same cell

The user moved to this thread at last - The ability to apply multiple formats to cell with xlwt / openpyxl

eladgl
  • 63
  • 8