Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用PythonDocx编写word文档时,如何更改段落中特定文本的字体?_Python_Python Docx - Fatal编程技术网

在使用PythonDocx编写word文档时,如何更改段落中特定文本的字体?

在使用PythonDocx编写word文档时,如何更改段落中特定文本的字体?,python,python-docx,Python,Python Docx,我正在使用Python解析excel电子表格,并将文档写入word文档。我想通过使数字显示为深红色来突出显示二进制子字符串,例如“001”。我可以使用re来查找子字符串,它是单引号之间的二进制数字序列,这不是我的问题。问题是我如何在段落中突出显示这些字符?我希望最终采用的格式如下: 任何帮助都将不胜感激 from docx import Document from docx.shared import RGBColor document = Document() run = docum

我正在使用Python解析excel电子表格,并将文档写入word文档。我想通过使数字显示为深红色来突出显示二进制子字符串,例如“001”。我可以使用re来查找子字符串,它是单引号之间的二进制数字序列,这不是我的问题。问题是我如何在段落中突出显示这些字符?我希望最终采用的格式如下:

任何帮助都将不胜感激

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')
这会将您的二进制代码的字体颜色更改为您提供的颜色代码。参考了解更多


这会将您的二进制代码的字体颜色更改为您提供的颜色代码。参考了解更多

感谢@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)

感谢@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)

Paandittya下面的代码显示了一个示例。关键概念是在运行级别指定字符格式(字体)。运行中的所有文本共享相同的字符格式。因此,您需要将二进制子字符串拆分为单独的运行,并将其格式设置为所需的外观。关键概念是在运行级别指定字符格式(字体)。运行中的所有文本共享相同的字符格式。因此,您需要将二进制子字符串拆分为单独的运行,并将其格式设置为所需的外观。谢谢您的回答。我认为b_run.font应该是二进制的吗?因此,答案是让“运行”中的段落更改要突出显示的位的字体?请注意,在我的示例中,第一个字符(单引号)是普通字体,因此我将在binary_run=statement?之前添加此字符,对代码中的错误表示诚挚的歉意。第7行实际上应该有
二进制运行
,而不是
二进制运行
。我已经编辑了代码。你说得对。是的,你说的没错,你可以互相排他性地设置每次跑步的格式,并使用
add\u run
继续加入他们。谢谢你的回答。我认为b_run.font应该是二进制的吗?因此,答案是让“运行”中的段落更改要突出显示的位的字体?请注意,在我的示例中,第一个字符(单引号)是普通字体,因此我将在binary_run=statement?之前添加此字符,对代码中的错误表示诚挚的歉意。第7行实际上应该有
二进制运行
,而不是
二进制运行
。我已经编辑了代码。你说得对。是的,您正确地指出,您可以使用
add\u run
相互独占地设置每次运行的格式,并继续加入它们。