是否有MS Word知道的docx特殊字符可以使单词斜体(使用python-docx)



我已经设置了一个python38脚本,该脚本读取样式设置为段落的模板docx文件,然后用我构建的新字符串替换文本。但是我现在需要在段落中使一些单词斜体,并且我试图避免进行大量重写以不同的方式构建段落。 所以我希望有另一种方法...

我可以使用MS Word识别并使特定单词斜体的特殊字符吗? 例如,我有以下代码:

for string in instructions.get_instructions()[instruction_key]:
string = string.replace('<number_of_items>',str(no_of_items))
string = string.replace('<item_name>', str(item_ref))
string = string.replace('<front_end>', front_end_ref_formatted)
string = string.replace('<back_end>', back_end_ref_formatted)
string = string.replace('<address>',address)
document.add_paragraph(string, style=style('SOW_Document_install_new_item'))

有没有类似的东西

for string in instructions.get_instructions()[instruction_key]:
string = string.replace('<<item_name> italics=True>', str(item_ref))
document.add_paragraph(string, style=style('SOW_Document_install_new_item'))

MS Word 在哪里看到斜体在文本中为 True?类似于'n'如何表示文本文件中的下一行?

根据 python-docx 用户指南,斜体在运行级别应用。段落中的所有文本都包含在一个或多个运行序列中。因此,似乎您需要将运行替换为序列运行,其中要格式化的单词包含自己的运行,然后将斜体应用于该运行。