-1

My problem is that my string color does not change, The strings name is "TitluCentru". I need to set the color to light blue.


import docx
from docx.enum.text import WD_ALIGN_PARAGRAPH

doc = docx.Document()

TitluCentru = doc.add_paragraph('NOTIFICARE') 
TitluCentru_format = TitluCentru.paragraph_format
TitluCentru_format.alignment
TitluCentru_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
TitluCentru_format.alignment

TitluStanga.font.color.rgb = RGBColor(0x22, 0x8b, 0x22)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I'm not familiar with docx, but I notice you do all the work to TitluCentru but change the font in TitluStanga. – OneMadGypsy Aug 11 '22 at 03:11

1 Answers1

0

I figured this out for you, but it is not awesome. This has to be the most convoluted package I have ever had to work with. I was determined to at least make it work, and I did!

I'm sorry, I really can't explain this to you with any confidence. All I can tell you is it works. I checked it in LibraOffice and 'NOTIFICARE' was green, just like I told it to be. Hopefully you can take this info and do something with it. I don't want to mess with this package, ever again. I can give you this tidbit, though. If you moved your text directly into add_paragraph but left everything else exactly how I have it, 'NOTIFICARE' will NOT change color. The color get's applied to whatever is in the run. So, I'm assuming if you have a lot of color changing to do, your program is going to be a whole lot of the hot mess, below.

I hope somebody else that knows what they are doing answers your question. I want to compare my answer with the "real one". I didn't find this info. I trudged through the docs storing tidbits and tried so many combinations that I eventually got lucky. I'm having a hard time believing it's this convoluted to assign a color to text. Honestly if I had to use this, I would just make the entire doc in html and throw it in a converter.

import docx
from docx.shared import RGBColor

doc = docx.Document()

r = doc.add_paragraph().add_run('NOTIFICARE')

r.font.color.rgb = RGBColor(0x00, 0xFF, 0x00)

doc.save('test.docx')
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
  • Thanks for the solution and for trying so hard to making it work –  Aug 11 '22 at 11:56
  • @IoshuaBerdei - You're welcome but this site doesn't work on thanks it works by you selecting answers with the big check mark to the left of answers. I just noticed that you've never selected ANY of the answers you were given. That's not right! You need to go select the answers that helped you.! That's 100% why people answer questions. That's why nobody else answered this question. You're making a name for yourself as someone to ignore. Go check the answers that solved your questions! 1 per question. – OneMadGypsy Aug 11 '22 at 16:03
  • I understand what are you saying but i cant up vote your question because I don t have enough reputation. –  Aug 11 '22 at 20:29
  • I see, thanks man I am going to check all the marks I didnt check . –  Aug 11 '22 at 20:53