-1

I want to color a text present in the string and pass the string to another python file so that to put received colored string into a docx file. I tried in this way but it is not working.

from termcolor import colored
from docx import Document

document = Document()
item_i="\n\n Comma is required in line dependent clause is in beginning\n\n" 
ctxt = colored(item_i, 'blue')
p=document.add_paragraph()
p.add_run(ctxt)
document.add_page_break()

document.save('demo.docx')

it displays properly in terminal but not in file, it shows an error

from termcolor import colored

item_i="\n\n Comma is required in line dependent clause is in beginning\n\n" 
ctxt = colored(item_i, 'blue')
print ctxt

In this format it displays properly. Kindly help me to resolve this issue.

martineau
  • 119,623
  • 25
  • 170
  • 301
Rejo Varghese
  • 67
  • 2
  • 9
  • 3
    I guess termcolor can color chars on a terminal. And I guess docx has something to do with microsoft word format. The two are unrelated. Coloring on a terminal is coded completely different from coloring chars in ms word. – Jacques de Hooge Oct 15 '16 at 14:38
  • yes but i want to do the same thing on a file – Rejo Varghese Oct 15 '16 at 14:39
  • Do you mean a file meant to be opened in msword or to be printed on a terminal (black console window) – Jacques de Hooge Oct 15 '16 at 14:40
  • I want to print that in ms word – Rejo Varghese Oct 15 '16 at 14:44
  • txt file can also work but docx is more preferred – Rejo Varghese Oct 15 '16 at 14:45
  • I think it will work in a txt file if you just echo it to the console. But using termcolor.colored will never work in .docx format for ms-word. A simple workaround is to produce a better documented, simpler format like HTML, which you can load into msword. You don't need to import termcolor or docs to do that. – Jacques de Hooge Oct 15 '16 at 14:46
  • I have learnt from this question, as well as, from the comments and answer to it. I don't know why some smart people have down-voted this question! I always though that the zeal to self assure has a huge preponderence among people of mediocrity like me. Somehow, smart people are displaying this unique idiosyncrasy all across stackoverflow. – cph_sto Feb 09 '18 at 07:55

1 Answers1

3

You should be using docx's text formatting since, as Jacques de Hooge said, termcolor is for terminal. See here.

from docx.shared import RGBColor

Then

run = p.add_run(item_i)
run.font.color.rgb = RGBColor(0x00, 0x00, 0xFF)
xli
  • 1,298
  • 1
  • 9
  • 30
  • Thank you for your effort. I've tried this also but I've to return the changes made in string to another file so that file can put the string into docx file. I mean when I give return item_i or run the changes made is to be there. – Rejo Varghese Oct 16 '16 at 04:08
  • I'm new to python that's why I'm having these types of doubts. – Rejo Varghese Oct 16 '16 at 04:09
  • Not sure what you mean--do you need to read a string from a text file and write it to the docx file? – xli Oct 16 '16 at 04:24
  • no i just want to write a string in which some of the text is colored and pass the string to another python file which creates a docx file of passed string. – Rejo Varghese Oct 16 '16 at 09:25
  • Why do you want to pass it to another file? You want to put this functionality in a different module? – xli Oct 16 '16 at 13:01