将文本从tkinter文本框中保存到.doc文件时,如何保持格式?

问题描述

我一直在尝试找到一种方法来保存我一直在使用的文本编辑器应用程序中Tkinter文本框中的格式。

这是我的粗体功能代码

def bold():

    boldFont = font.Font(textBox,textBox.cget("font"))
    boldFont.configure(weight = "bold")

    textBox.tag_configure("bold",font = boldFont)

    currentTags = textBox.tag_names("sel.first")

    if ("bold" in currentTags):
        textBox.tag_remove("bold","sel.first","sel.last")

    else:
        textBox.tag_add("bold","sel.last")

我使用以下代码调用函数

boldButton = Button(toolbarFrame,text = "B",command = bold,font = ('Times New Roman',11,'bold'))

当我选择文本并按下按钮时,该按钮在文本框中起作用,但是当我保存文件时,格式会被删除。我将其保存到.doc文件

我使用以下功能将其保存为.doc文件

def saveNote():

    name = str(nameBox.dump('1.0',END))
    f = open(name + ".doc","w")

    text = str(textBox.dump('1.0',"a")

    f.write(text)

    f.close()

我尝试同时使用get()函数dump()函数,但都没有起作用。

保存文件时如何保持格式?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)