我使用Python来解析excel电子表格并将文档写入word文档。我想突出显示二进制子串,例如'001',数字显示为深红色。我可以使用re是任何文本找到子字符串,这是单引号之间的二进制数字序列,这不是我的问题。问题是如何突出显示段落中的那些字符?我想最终得到的格式如下:
任何帮助都将不胜感激。
答案 0 :(得分:3)
from docx import Document
from docx.shared import RGBColor
document = Document()
run = document.add_paragraph()
binary_run = run.add_run('your_binary_code')
binary_run.font.color.rgb = RGBColor(rgb_color_code_goes_here)
cmplt_run = binary_run.add_run('rest of the text goes here')
这会将您的二进制代码的字体颜色更改为您提供的颜色代码。请参阅python-docx documemtation了解详情。
答案 1 :(得分:0)
感谢@Paandittya的灵感,这是我的工作代码:
def highlight_binary(par, text):
''' Add text to paragraph while highlighting binary in dark red
intputs:
par => the paragraph to add text to
text => the text with 0 or more binary strings in it
'''
BINARY_STR = re.compile("'[01]+'")
for occurance in re.findall(BINARY_STR, text):
pos = text.find(occurance)
par.add_run(text[:pos+1]) # +1 -> opening quote in normal text
text = text[pos+len(occurance)-1:] # -1 -> closing quote in normal text
hl = par.add_run(occurance[1:-1]) # remove quotes from highlighted part
hl.font.color.rgb = RGBColor(192,0,0)
if text: # any text left?
par.add_run(text)