0

I'm writing a program in Python where you can write to a text file from the program, remotely. Relevant Code:

namein = input("What do you want the filename to be? Don't include an extension...\n")
extin = input("What would you like the extension to be? This program supports:\nMicrosoft Word Document: '.docx'\nPlain Text File: '.txt'\nRich Text File: '.rtf'\nPages Document: '.pages'\n")
aw = input("Do you want to append your text file, or rewrite the whole thing? (append/write) ")
if aw == 'append':
    textin = input("In the line below, write the text you want to put into your text document!\n\n")
    outfile = open(namein + extin, 'a')
    outfile.write(textin)
    outfile.flush()
    print("Great! Now your text file has been updated!")
    print("Your text file:\n")
    outfile.close()
    outfile = open(namein + extin, 'r')
    print(outfile.read())

When someone chooses a non '.txt' file, one can't open the file! It just says error, the file can't be opened. Is there a way around this?

Billjk
  • 10,387
  • 23
  • 54
  • 73

3 Answers3

1

From your code it looks like "textin" contains regular text, which maybe you read from the keyboard. You can't create a Word document that way: Word documents contain headers, font information etc., so you need to go through a library for each kind of document. For example, to write an Excel spreadsheet you can use the xlwt python module. For Word documents, see this SO question. There is a python library for RTF files, pyrtf.

Is this what you were doing or have I misunderstood your simplified code?

PS. Do NOT enable any web interface that lets strangers write files on your server for at least, let's say, a year. There are ways to do that reasonably safely, but I'm confident that you are not yet ready for that yet. (See the other comments for hints of the kind of trouble you'll get into).

Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161
  • For pyrtf, do i just do import pyrtf and it magiacally works? – Billjk Mar 07 '12 at 23:00
  • 1
    @user1247509: No, as with all libraries, you need to look at the documentation and study the API, then mess around with it a bit and integrate it into your code. – Niklas B. Mar 07 '12 at 23:01
  • 1
    Briefly: They provide commands that will generate the text for you. It will be complicated. My advice is to try this: Use Word to create a document with one word: "Hello". Save it and open the document with Notepad. This is the kind of thing you need to create. My advice is to put binary formats aside for a couple of months and work on something else first. – alexis Mar 07 '12 at 23:06
1

Your code is bass ackwards. You should be asking them what sort of file they want to save the text as and set the extension based on that. Them have to put the dot in isn't good either...

After that while you'll get away with saving straight text, with an rtf extension or doc. DocX and pdf are not going to work. you'd need to create document of that type and then add the text as content.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

i imagine (but am only guessing as i don't use windows much) that the other formats are treated as binary files. in which case you need to open as a binary file:

outfile = open(namein + extin, 'ab')

however, i guess there may be restrictions on appending to binary files...? [apparently not - thanks alexis]. also, as everyone is pointing out: if they are binary files, then this isn't likely to do much good.

more generally, i just want to say, despite what you read here, this is how we all learnt. by playing around and doing stuff and screwing up and learning what not to do. i find the smarter-than-thou, put-you-down style of Niklas to be particularly grating. he might not admit it, but even he would once have written silly little programs and - shock - not known all the answers. keep learning. your program is cool. and if it was perfect, what would you have learnt?

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • So, you are saying that a `.docx` or a `.rtf` with just plain text in it will work as expected? I doubt that really much. – Niklas B. Mar 07 '12 at 22:35
  • Yes, but that is what OP seems to be expecting, and you don't describe how this is absolutely not the case and how just opening the file in binary mode will not help at all. – Niklas B. Mar 07 '12 at 22:38
  • you're free to post a better answer yourself, you know. personally, i think most people learn in steps. but if you want to show off and shove your knowledge down everyone's throat go ahead. really, i am not stopping you. – andrew cooke Mar 07 '12 at 22:40
  • There are no restrictions on appending to binary files. Append is append. – alexis Mar 07 '12 at 22:47
  • Aye but if you do a straight append to a structured file, you'll almost certainly break the structure. Wouldn't work if you started with template docx or pdf. At best it would ignore it. – Tony Hopkinson Mar 07 '12 at 22:53
  • 1
    He doesn't seem to *have* any structure, that's the real problem :-) – alexis Mar 07 '12 at 23:00
  • Whaddata mean? How can i improve it? – Billjk Mar 07 '12 at 23:12