【问题标题】:Is there a special character for docx that MS Word knows to make a word italics (using python-docx)docx 是否有一个特殊字符,MS Word 知道使单词斜体(使用 python-docx)
【发布时间】:2020-06-15 23:52:03
【问题描述】:

我设置了一个 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】:

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

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2018-03-24
      • 2021-06-25
      相关资源
      最近更新 更多