Scripting.FileSystemObject Scripting.FileSystemObject - 1 month ago 17
Python Question

how to generate RGBcolor in RGBColor(0x42, 0x24, 0xE9) format

I am working with python docx and here I am stuck.

from docx import Document
document = Document()
run = document.add_paragraph().add_run()
font = run.font
from docx.shared import RGBColor
font.color.rgb = RGBColor(0x42, 0x24, 0xE9)


This generate RGB(66, 36, 233) when viewed from microsoft word.

how can I get RGBColor(0x42, 0x24, 0xE9) type color given RGB(66, 36, 233) format ?

Answer

The three parameters to RGBColor are just integers, so:

RGBColor(66, 36, 233)

Produces the same results as:

RGBColor(0x42, 0x24, 0xE9)

The 0x prefix is just a way of telling Python that what follows is to be interpreted as a base-16 number. It doesn't have to do with python-docx per se; any way you get an integer between 0 and 255 into those three positions will work fine.