-1

I am using python 3.5, python-docx, and TKinter. Is there any way to be able to type a .txt file name into an entry box and have python insert the contents of that .txt file into a specific place in docx? I know how to get information from Entry boxes in tkinter and how to convert them into a string.

I want to be able to enter someones name (which would also be the name of the text file) into the entry box and have python insert the content of the .txt file.

Thanks

Update here is my code that I'm trying

from tkinter import *
from tkinter import ttk
import tkinter as tk
from docx import Document
root=Tk()

def make_document():
    testbox=ProjectEngineerEntry.get()
    TestBox=str(testbox)

    def projectengineer():
        with open(+TestBox+'.txt') as f:
            for line in f:
                document.add_paragraph(line)
    document=Document()

    h1=document.add_heading('engineer test',level=1)
    h1a=document.add_heading('Project Engineer',level=2)
    projectengineer()

    document.save('test.docx')

notebook=ttk.Notebook()
notebook.pack(fill=BOTH)
frame1=ttk.Frame(notebook)
notebook.add(frame1,text='Tester',sticky=W)
tester1=Label(frame1,text='Test1')
ProjectEngineerEntry=Entry(frame1)
tester1.pack()
ProjectEngineerEntry.pack()
save=Button(frame1,text='Save',command=make_document).pack()

As you can see I am trying to take the information from the entry box, convert it to a string and then use that string to open a text file with that specific name. However I keep getting

a TypeError: bad operand type for unary +: 'str'

I don't understand whats going on here. In the actual document I used the ++ method when saving the file (saves it as the current date and time).

Craig Walker
  • 131
  • 2
  • 10
  • So what is your problem? Creating a filename from the entered text? Reading the contents of the file? Adding it to the .docx? Have you attempted to code this yourself? – SiHa Mar 29 '17 at 07:13
  • I had an issue calling the specific text file from an entry box string. – Craig Walker Mar 30 '17 at 04:02
  • Updated original question with code I tried if that helps. – Craig Walker Mar 30 '17 at 17:13
  • Why do you have the '+' sign in front of `TestBox` in line 12? I suspect that's where your error is coming from. – scanny Mar 30 '17 at 17:30
  • That's annoying. I don't know why I had the + before the `TestBox` I took that out and it works perfect now. Thanks – Craig Walker Mar 30 '17 at 20:56

3 Answers3

5

python-docx doesn't have this functionality and it's unlikely it ever will. Typically this sort of thing is done to suit by the script or application using python-docx. Something like this:

from docx import Document

document = Document()
with open('textfile.txt') as f:
    for line in f:
        document.add_paragraph(line)
document.save('wordfile.docx')

You'll need to deal with the particulars, like how a paragraph is separated (perhaps a blank line) and so on, but the code doesn't need to be much longer than this.

scanny
  • 26,423
  • 5
  • 54
  • 80
1

Take a look at the following python library: https://python-docx.readthedocs.io/en/latest/

hasanzuav
  • 554
  • 5
  • 15
  • Looked at the library and didn't help. I know how to enter text with docx but for simplicity I'm looking for a way to pull it out of a text file. Apparently people would rather edit txt files than py files. – Craig Walker Mar 28 '17 at 12:19
  • Post your attempt to better help me craft an answer. – hasanzuav Mar 30 '17 at 05:49
  • I posted my code attempt in the original question if that helps you out. Thanks – Craig Walker Mar 30 '17 at 17:13
0

just a pretty adaptation from scanny answer

%pip install python-docx

from docx import Document

def to_docx(file_path_plain_text, file_path_docx):
    
    document = Document()
    with open(file_path_plain_text) as f:
        for line in f:
            document.add_paragraph(line)
    document.save(file_path_docx)

to_docx('my_file.txt', 'report.docx')

Possible charmap and encoding errors may arise in capturing the content of the plaintext file. But writing a robust function about this capture would not be part of the scope of this topic.

the_RR
  • 311
  • 2
  • 9