question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to change the color of different words in a long text string?

See original GitHub issue

I know how to change the font color with:

worksheet.write_rich_string(‘A3’, 'This is ', red, ‘red’, ’ and this is ', blue, ‘blue’)

Question 1:

How to use write_rich_string in a for loop (in the same cell)? The purpose here is to set the color of words in a long string according to certain condition. How to change the color of each word and append them one by one in the same cell? For example:

for word in words:
         if word in a:
              worksheet.write_rich_string(1, 1, word, red)  # red color
         else:
              worksheet.write_rich_string(1, 1, word, blue)  # blue color

Question 2:

As the above string is too long, how to wrap the text in that cell based on the results in question 1?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
jmcnamaracommented, Apr 9, 2016

@leefionglee You should show a small complete working program that demonstrates the issue.

Python only allows named parameters afters a *args parameter. In this case there isn’t a names parameter so you should include or append the cell format. Like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
worksheet = workbook.add_worksheet()

red = workbook.add_format({'color': 'red'})
blue = workbook.add_format({'color': 'blue'})
text_wrap = workbook.add_format({'text_wrap': True})

string_parts = [ 'This is ', red, 'red', ' and this is ', blue, 'blue']

string_parts.append(text_wrap)

worksheet.write_rich_string('A1', *string_parts)

workbook.close()

Output:

screenshot

0reactions
hamedsheyghcommented, Aug 4, 2021

i want some thing just like that but in tkinter not in xlsxwriter any one know how to do that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the Color of Words in Text - My Online Training Hub
Change the color of each of those words (to different colors if you like); Check a single cell, or any size range; Check...
Read more >
HTML: Changing colors of specific words in a string of text
formatting the text string, but want to change the color of "January 30, 2011" to #FF0000 and "summer" to #0000A0. How do I...
Read more >
Changing font color of certain repeated words in a document
Open it and type in the name for Find. · Type ^& in the Replace box (means found text). · Then click on...
Read more >
How to change the Text Color of a Substring in android using ...
It is easy to change the color of the whole string but to change the color of a substring we have to use...
Read more >
How to Change the Text Color of a Substring - Android Studio ...
In this video we will learn, how to use the SpannableString and SpannableStringBuilder classes together with ForegroundColorSpan and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found