alexandru.titirig
alexandru.titirig

Reputation: 3

Extract text and write to new Word file

I am trying to make a GUI Tkinter, with 2 buttons, every button is to: extract paragraph (1,2,3 whatever) and copy to new text file (Pyhton 3.8, PyCharm (Windows 10, OS). But the thing is that, it extracts, copy to new file, but does't add, it only make extracts/copy to new file, one button and not both of them.

here is the code (keep in mind that I am totaly amateur, with only 2 weeks beeing activ in Python :D)

from tkinter import *

import docx
from docx import Document

doc = docx.Document("C:/Users/Dorian/Desktop/Descriere/1/texte_aparate_de_aer_conditionat.docx")

def intro_text():

    document = Document()

   single_para1 = doc.paragraphs[2]
    single_para2 = doc.paragraphs[4]

    document.add_paragraph(single_para1.text)
    document.add_paragraph(single_para2.text)
    document.save('C:/Users/Dorian/Desktop/Descriere/1/txt.docx')

  def specs():

    document = Document()

    single_para3 = doc.paragraphs[6]
    single_para4 = doc.paragraphs[16]

    document.add_paragraph(single_para3.text)
    document.add_paragraph(single_para4.text)
    document.save('C:/Users/Dorian/Desktop/Descriere/1/txt.docx')

main=Tk()
main.title('Descriere Aparate de Aer Conditionat')
main.geometry('400x100')

GUIFrame=Frame(main)
GUIFrame.grid(row=1, column=1, sticky=W)

Button(GUIFrame, text="Pentru cele cu Wi-Fi", width=35, command=intro_text).grid(row=1, column=1, sticky=W)
Button(GUIFrame, text="Eficienta", width=35, command=specs).grid(row=3, column=1, sticky=W)

main.mainloop()

Thank you!

Upvotes: 0

Views: 149

Answers (1)

Ronald
Ronald

Reputation: 3325

If I correctly understand your question, you want to add paras to a new document. As far as I can tell, both buttons create a new document, but they save it with the same name. Therefore you keep overwriting the document each time you press a button.

You should create separate functions: one for creating a new document, then the two that add paragraphs to that document, and finally a function that saves the document.

Upvotes: 1

Related Questions