MS Word是否知道docx的特殊字符以斜体字表示(使用python-docx)

问题描述 投票:0回答:1

我已经设置了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 fonts docx
1个回答
0
投票

python-docx user guide,斜体在运行级别应用。段落中的所有文本都包含在一个或多个运行序列中。因此,似乎您需要用序列运行替换运行,其中要格式化的单词包含它们自己的运行,然后对该运行应用斜体。

© www.soinside.com 2019 - 2024. All rights reserved.