[英]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.我正在尝试制作一个带有 2 个按钮的 GUI Tkinter,每个按钮用于:提取段落(1、2、3 等等)并复制到新的文本文件(Pyhton 3.8、PyCharm(Windows 10、操作系统)。但事情是那,它提取,复制到新文件,但不添加,它只提取/复制到新文件,一个按钮而不是两者。
here is the code (keep in mind that I am totaly amateur, with only 2 weeks beeing activ in Python:D)这是代码(请记住,我完全是业余爱好者,在 Python:D 中只有 2 周的beeing 活动)
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!谢谢!
If I correctly understand your question, you want to add paras to a new document.如果我正确理解您的问题,您想将 paras 添加到新文档中。 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.您应该创建单独的函数:一个用于创建新文档,然后两个用于向该文档添加段落,最后一个用于保存文档的 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.