您可以使用 Python-docx 为同一 运行 上的段落部分添加下划线吗?

Can you use Python-docx to underline part of a paragraph that is on the same run?

我是 python 新手,也是编程新手,所以请原谅我的无知。

我正在使用 python-docx 根据需要自动格式化文档。在我们的数据库应用程序中,我们有大量定期分批更新的表单。它们都遵循几乎相同的格式,我们得到的是新更新的文档,其格式不符合我们的需要。

所以我有几个关于我正在尝试做的事情的问题: 1)在每个文档中,文档开头都有一个数字,比如5.1。在数字之后,我需要放置一个制表符,然后在该段落的其余部分下划线。我想不通,也许我看它的方式是不可能的,但我不能在某个地方放一个标签,也不能弄清楚如何给段落的其余部分加下划线,因为只有一个 运行 而已,我找不到任何方法将单个 运行 分成两个 运行。我能够做的是打开文档并使用 pyautogui 在循环中使用 pyautogui.press('right') 将空格数移动到右边第 1 段。但我认为这不是首选。 我想也许我可以将文本插入一个字符串,然后将数字从其余的单词中拆分出来,然后使用 python-docx 删除旧文本,然后插入具有不同格式的新文本( 运行 秒)。这是最好的方法还是有更好的方法? 这就是我目前执行此任务的方式,但它不允许我大胆。我想使用 python-docx 执行整个任务,这样我就不会依赖使用 gui 来进行更改

def JITitleNumberLength():
    doc = docx.Document('1ji.docx')
    p0 = doc.paragraphs[0]
    p0Size = len(p0.text) #finds length of title in paragraph 0
    JI_Title = p0.text
    JI_Title_List = list(JI_Title)
    #print(JI_Title_List[2])
    JI_Index_Length = 0 #Returns the amount of numbers in the title of the Jury Instruction
    counter = 0
    while (counter < p0Size) and True:
        #print(JI_Title_List[counter], ' ', JI_Index_Length)
        if (JI_Title_List[counter] == '1' or
        JI_Title_List[counter] == '2' or
        JI_Title_List[counter] == '3' or
        JI_Title_List[counter] == '4' or
        JI_Title_List[counter] == '5' or
        JI_Title_List[counter] == '6' or
        JI_Title_List[counter] == '7' or
        JI_Title_List[counter] == '8' or
        JI_Title_List[counter] == '9' or
        JI_Title_List[counter] == '0' or
        JI_Title_List[counter] == '.'):
            #print('If Statement True')
            JI_Index_Length = JI_Index_Length + 1
        else:
            #print('False')
            False
        counter = counter + 1
    return JI_Index_Length


def OpenDocumentForAutoGUI():
    os.system("start " + '1ji.docx')
    time.sleep(1) #causes delay to allow document to full open before next command runs

def main():
    TitleNumberLength = int(JITitleNumberLength())
    for i in range(TitleNumberLength):
        pyautogui.press('right')
    pyautogui.press(['delete', 'tab']) #removes space and inserts tab between number and instruction name

2) 在一段中间,会给出不同的选项,格式为[option 1] [option 2] [option 3]。我想创建一个内容控件,提供这三个选项的下拉选项。没有我读过的地方有关于 docx 内容控制的内容。有什么办法可以做到这一点,或者只是使用 pyautogui 手动执行此操作是我唯一的选择?基本上我的想法是,我会在段落中搜索括号 [],然后以某种方式将它们输入到内容控件中,如果需要的话,使用 pyautogui,如果可能的话我宁愿远离它。

我还没开始写这部分的代码,我唯一的想法是把每个选项都放在一个列表中,然后在使用pyautogui手动移动鼠标点击word中的开发人员选项卡后从列表中调用然后 select 内容控件,因为没有用于引入内容控件的键盘快捷键。我真的不想这样做,因为屏幕分辨率起着很大的作用,只有特定的屏幕分辨率才有效。

抱歉 - 我很确定 run-level 格式是您可以获得的最精细的格式。添加代码以在段落中创建第二个 运行 并对其应用下划线样式应该很简单。

不知道 drop-down 个列表框

两个风格提示:

您可以使用 'in' 和常量 string.digits 以及连接运算符来简化您的非常长的 if 语句

if JI_Title_List[counter] in (string.digits+'.') ....

你可以用+=表示x = x +;例如x+=1 是 x = x + 1

counter += 1
JI_Index_Length +=1