0

I have to write a multicolor text into an excel file. Following is the code I used:

import xlwt
filename = "file1"+".xls"
workbook = xlwt.Workbook(filename)
worksheet = workbook.add_sheet('GS')
bold = xlwt.easyxf('font: bold 1')
worksheet.write(0,1, 'Row Number', bold)
worksheet.write(0,2, 'Errors', bold)
worksheet.write(0,3, 'Warnings', bold)
worksheet.write(0,4, 'Result', bold)
style_pass = xlwt.easyxf('font: colour green, bold True;')
style_fail = xlwt.easyxf('font: colour red, bold True;')
row = 0
col = 0
for i in col:
    for x in row:
        try:
           text = str(self.tableWidget.item(row, col).text())
           substring = "HAVING"
           if substring in text:
                x = text.split("HAVING ")
                y = x[0].bold
                z = worksheet.write(row+1, col, x[1], style_pass) #need to make x[1] in green color
                updated_text = y + substring + z
                if(x[0] != ''):
                    worksheet.write(row+1, col, updated_text) #need to write x[0] in bold and merge the strings with x[1] in green color
            else:
                worksheet.write(row+1, col, text)
workbook.save(filename)

In that particular row and col , updated_text should be written in the prescribed manner.

FRECEENA FRANCIS
  • 181
  • 2
  • 13
  • Duplicate for this: https://stackoverflow.com/questions/15649482/how-to-set-color-of-text-using-xlwt ? – barbwire Jul 23 '21 at 06:45
  • 1
    I have to merge different colored or formatted text to same cell location. How will that be possible. – FRECEENA FRANCIS Jul 23 '21 at 08:49
  • @FRECEENAFRANCIS refer this https://stackoverflow.com/questions/49522658/openpyxl-change-one-words-color-in-the-same-cell – Tokci Apr 12 '22 at 06:51

0 Answers0