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.