Advertisement
Guest User

Word Challenge of Doom

a guest
Nov 12th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. #Christian Grey November of doom also Erek has caused me to loose my mind
  2.  
  3. import zipfile
  4. from xml.etree import ElementTree as ETree
  5. import time
  6.  
  7. #As always
  8. def preamble():
  9.     import random
  10.     while random.randint(1,10) >= 4:
  11.         print("Python bad")
  12.  
  13. def file_into_list(filepath, seperator):
  14.     handle = open(filepath, "r") #It's a file handle
  15.     return list(handle.readline().split(seperator)) #Only the first line of the file cause reasons
  16.  
  17. def mean_of(list):
  18.     _sum = 0 #Underscore since there's already a function called sum T.T
  19.     for nu in list:
  20.         _sum += nu #Add up all the things
  21.     return _sum / len(list) #Divide it by the number of things
  22.  
  23. def my_random_file_function(path, replace, replace_with):
  24.     #Erek challenged me to open a word document and replace all instances of something with something
  25.     if not zipfile.is_zipfile(path):
  26.         print("Clown this isn't a word file")
  27.         return
  28.     file = zipfile.ZipFile(path, mode = "a") #Apparently .docx files are actually xml files within zip files; Mode = a for read and write
  29.     try:
  30.         file.namelist().index("word/document.xml") #Make sure this particular file contains the target word document xml
  31.     except:
  32.         print("Clown this isn't a word file")
  33.         return
  34.     parsed = ETree.fromstring(file.read("word/document.xml")) #Parse the XML
  35.     for el in parsed.iter(): #Go through every node in existance
  36.         if isinstance(el, ETree.Element): #Make sure it's an element
  37.             if el.tag == "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t": #Check the tag name that was mangled by the parser
  38.                 el.text = el.text.replace(replace, replace_with) #Replave the text
  39.     file.writestr("word/document.xml", ETree.tostring(parsed)) #Write out the new file
  40.     file.close()
  41.  
  42. def into_int(val):
  43.     return int(val)
  44.  
  45. def main():
  46.     while True:
  47.         preamble()
  48.         print("\nMenu of doom:")
  49.         print("1) Mean of a CSV file")
  50.         print("2) Find and replace in a word file")
  51.         print("Anything else exits")
  52.  
  53.         in2 = input("\n> ")
  54.         if in2 == '1':
  55.             pathname = input("CSV file name to read from. It better be a valid \nCSV file cause we don't actually check\nFilename> ")
  56.             li = list(map(into_int, file_into_list(pathname, ",")))
  57.             print(li)
  58.             print("Mean>", mean_of(li))
  59.         elif in2 == '2':
  60.             pathname = input("Word file to read in. Better be an actual word file cause \nwe don't actually check.\nFilename> ")
  61.             replace = input("Search for> ")
  62.             replace_with = input("Replace with> ")
  63.             my_random_file_function(pathname, replace, replace_with)
  64.         else:
  65.             break;
  66.         time.sleep(1.5)
  67.  
  68. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement