Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Christian Grey November of doom also Erek has caused me to loose my mind
- import zipfile
- from xml.etree import ElementTree as ETree
- import time
- #As always
- def preamble():
- import random
- while random.randint(1,10) >= 4:
- print("Python bad")
- def file_into_list(filepath, seperator):
- handle = open(filepath, "r") #It's a file handle
- return list(handle.readline().split(seperator)) #Only the first line of the file cause reasons
- def mean_of(list):
- _sum = 0 #Underscore since there's already a function called sum T.T
- for nu in list:
- _sum += nu #Add up all the things
- return _sum / len(list) #Divide it by the number of things
- def my_random_file_function(path, replace, replace_with):
- #Erek challenged me to open a word document and replace all instances of something with something
- if not zipfile.is_zipfile(path):
- print("Clown this isn't a word file")
- return
- file = zipfile.ZipFile(path, mode = "a") #Apparently .docx files are actually xml files within zip files; Mode = a for read and write
- try:
- file.namelist().index("word/document.xml") #Make sure this particular file contains the target word document xml
- except:
- print("Clown this isn't a word file")
- return
- parsed = ETree.fromstring(file.read("word/document.xml")) #Parse the XML
- for el in parsed.iter(): #Go through every node in existance
- if isinstance(el, ETree.Element): #Make sure it's an element
- if el.tag == "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t": #Check the tag name that was mangled by the parser
- el.text = el.text.replace(replace, replace_with) #Replave the text
- file.writestr("word/document.xml", ETree.tostring(parsed)) #Write out the new file
- file.close()
- def into_int(val):
- return int(val)
- def main():
- while True:
- preamble()
- print("\nMenu of doom:")
- print("1) Mean of a CSV file")
- print("2) Find and replace in a word file")
- print("Anything else exits")
- in2 = input("\n> ")
- if in2 == '1':
- pathname = input("CSV file name to read from. It better be a valid \nCSV file cause we don't actually check\nFilename> ")
- li = list(map(into_int, file_into_list(pathname, ",")))
- print(li)
- print("Mean>", mean_of(li))
- elif in2 == '2':
- pathname = input("Word file to read in. Better be an actual word file cause \nwe don't actually check.\nFilename> ")
- replace = input("Search for> ")
- replace_with = input("Replace with> ")
- my_random_file_function(pathname, replace, replace_with)
- else:
- break;
- time.sleep(1.5)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement