I made a program that has no REAL purpose, but i did it solely to see if i could do it, because i am new to python (somewhat) and i did:

It will open a .txt file, and add a word of your choice inbetween each word that exists in the file.

import pickle

dashes = "==================================="

hostageFile = raw_input("Which file to mess up? ")
#opens the selected file
textf = open(hostageFile, 'r')
str1 = textf.read()
textf.close()

#makes every word in file a list
wordlist = str1.split(None)

#shows the unaltered file
print dashes
print str1
print dashes

#prints every word in the file as a list
print dashes
print wordlist
print dashes

#grabs a some characters for the manipulating
password = raw_input("Enter some characters: ")

#this makes it so that the number of words in the document doesn't matter, it grabs
#the number of words and uses that number for the manipulation using len(wordlist)
x = len(wordlist)

#when adding the words, the list will get twice the size, so the words will only
#go to the size of the previous list, meaning it will only go half way down the
#new list. this makes it go the rest of the way
y = x*2

#makes it so that the number of words in document is again irrelivent
#goes until the number y is reached
counter = 0
while counter != y:
    wordlist.insert(counter, password)
    counter += 2
    if counter == y:
        break

#prints modified wordlist
print dashes
print wordlist
print dashes

#turns the list into a string with a space inbetween each word
joinList = " ".join(wordlist)

#prints the string
print dashes
print joinList
print dashes

#asks user if they are sure they wish to modify
sure = raw_input("Modify?(y or n): ")
if sure == "y" or sure == "Y":
    #saves the new string over the old file's words, essentially modifying the contents
    file = open(hostageFile, "w")
    pickle.dump(joinList, file)
    file.close()
    print "modified..."
#if no, the program restarts
elif sure == "n" or sure == "N":
    execfile("File content changer.py")

but when i try to tell it to do this to a .doc instead of a .txt it does it horribly wrong. is there any way i can make it able to manipulate .doc aswell as .txt

Recommended Answers

All 3 Replies

If with .doc file you mean a MS Word file, no way! Just look at a typical Word .doc file with an editor and you will know why.

I second Vegaseat on that. I looked into the issue once in my pre-Python days and ended up with 300+ pages of documentation on OLE files. Basically, a .doc is stored not only with the text, but also with the entire edit history.

My understanding ... someone correct me if I'm wrong ... is that the next version of Office will use something like XML, which will make those files theoretically within the reach of Python. Woo-hoo!

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.