简体   繁体   English

提取文本并写入新的 Word 文件

[英]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.

相关问题 从文档中提取文本块并将其写入新的文本文件 - Extract chunks of text from document and write them to new text file 如何编写一种从文件目录中的Word文档提取文本的方法(采用文件目录)? - How to write a method (that takes in a file directory) to extract text from word documents in a file directory? Python:从文本文件中提取节并将其写入新的单独文本文件 - Python: extract sections from a text file and write them into new separate text files 如何从 docx 文件中提取文本和图像并将其写入新的 docx 文件,只需进行少量更改 - How to extract text and images from a docx file and write it to new docx file with few changes 从一个文本文件中提取日期并使用python写入新文件 - extract date from one text file and write to new file using python 对于文本文件中的每个单词,请提取周围的5个单词 - For each word in the text file, extract surrounding 5 words 拆分文本并写入新文件 - split text and write to new file 如何读取文本文件并将其逐字写入python中的另一个文件? - How to read a text file and write it word by word into another file in python? 在文本文件中搜索一行并在其前面写一个单词 - Search a line in text file and write a word before it 从文本文件中提取字符串并将其写入 Excel - Extract strings from text file and write it to Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM