# a very simple Tkinter editor to show file read/write dialog
 
from tkinter import *
from tkinter.filedialog import *
 
class App(object):
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.text = Text()
        self.text.pack()
 
        menu = Menu(master)
        root.config(menu=menu)
        # file menu
        filemenu = Menu(menu, tearoff=0)
        menu.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="New")
        filemenu.add_command(label="Open", command=self.file_open)
        filemenu.add_command(label="Save", command=self.file_save)        
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.do_exit)

        DADOS_botão = Button(master,text="CRIAR\nDOCUMENTO",bg='navy',foreground="white",font=("Arial", 12,"bold"),justify = LEFT,command=self.tags)
        DADOS_botão.pack()
        
    def file_open(self):
        """open a file to read"""
        # optional initial directory (default is current directory)
        initial_dir = "C:\Temp"
        # the filetype mask (default is all files)
        mask = \
        [("Text and Python files","*.txt *.py *.pyw"), 
        ("HTML files","*.htm"), 
        ("All files","*.*")]        
        fin = askopenfile(initialdir=initial_dir, filetypes=mask, mode='r')
        text = fin.read()
        if text != None:
            self.text.delete(0.0, END)
            self.text.insert(END,text)
    def tags(self):

        self.text.tag_add("oficio","1.0","2.0")
        self.text.tag_config("oficio",font=("Arial", 12,"bold"))

        
    def file_save(self):
        """get a filename and save the text in the editor widget"""
        # default extension is optional, here will add .txt if missing
        fout = asksaveasfile(mode='w', defaultextension=".doc")
        text2save = str(self.text.get(0.0,END))
        fout.write(text2save)
        fout.close()
 
    def do_exit(self):
        root.destroy()
 
root = Tk()
root.title("a very simple editor")
app = App(root)
root.mainloop()

i want to save the text with bold , but when i save the text the tag was lost


how to get the tag with the text to save?

Nobody seems to know, it's your third thread with the same question. If your goal is to write microsoft word files with python, I found this link https://github.com/mikemaccana/python-docx . Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.