from ftplib import parse150 import os, sys, docx from string import punctuation #Text file handler def paragraph_parser(contents): punctuation = [".", "?", "!", '"'] paragraph_end = [".\n\n", "?\n\n", "!\n\n", '"\n\n', "|E"] article_end = '\n' paragraph_index = [0] article_index = [0] paragraph_number = 1 paragraph=[] article=[] x = 0 contents=contents.strip() contents+="|E\n" while x in range (len(paragraph_end)): if contents.find(paragraph_end[x]) == -1: paragraph_end.pop(x) else: x += 1 while paragraph_end: minimum = contents[paragraph_index[paragraph_number-1]:].index(paragraph_end[0]) +paragraph_index[paragraph_number-1] for x in paragraph_end: if contents[paragraph_index[paragraph_number-1]:].index(x)+paragraph_index[paragraph_number-1]+1 < minimum: minimum = contents[paragraph_index[paragraph_number-1]:].index(x)+paragraph_index[paragraph_number-1] paragraph_index.append(minimum+1) paragraph.append(contents[paragraph_index[paragraph_number-1]:paragraph_index[paragraph_number]]+'

') for x in paragraph_end: if contents[paragraph_index[paragraph_number]:].find(x) == -1: paragraph_end.remove(x) first_nl =contents[paragraph_index[paragraph_number-1]+2:paragraph_index[paragraph_number]].index(article_end)+paragraph_index[paragraph_number-1]+2 if contents[first_nl-1:first_nl] not in punctuation: article_index.append(contents[paragraph_index[paragraph_number-1]+2:paragraph_index[paragraph_number]].index(article_end)+paragraph_index[paragraph_number-1]) paragraph_number +=1 paragraph_number = 0 for x in range(1,len(article_index)): if contents[article_index[x-1]:article_index[x]-2].rfind('\n') != -1: article.append(contents[contents[:article_index[x]].rfind('\n')+1:article_index[x]+2]) elif x==1: article.append(contents[contents[:article_index[x]].rfind('\n')+1:article_index[x]+2]) for x in article_index[1:]: while x not in range(paragraph_index[paragraph_number],paragraph_index[paragraph_number+1]): paragraph[paragraph_number]="

"+paragraph[paragraph_number] paragraph_number +=1 paragraph[paragraph_number]=paragraph[paragraph_number].replace("\n"," ") paragraph[paragraph_number]=paragraph[paragraph_number].strip() paragraph[paragraph_number]="

"+article[article_index.index(x)-1]+"

\n

"+paragraph[paragraph_number][len(article[article_index.index(x)-1]):] print("Header "+str(x)+" is in paragraph "+str(paragraph_number)) paragraph[-1] = paragraph[-1][:-5]+paragraph[-1][-4:] return ''.join(paragraph) #Document file Handler def dochandler(path): doc = docx.Document(path) fullText = [] for para in doc.paragraphs: fullText.append(para.text) print(para.text, para.style.name) return '\n'.join(path) def txthandler(path, htmlfile): filename = path[path.rfind('/')+1: path.rfind('.')] f = open(path, "r") contents = f.read() title = filename main_header = contents[0:contents.find('\n')] paragraphs = paragraph_parser(contents[contents.find('\n'):]) htmlfile.write('' '' '' '' +title+ '' '' '' '' '' '' '

' +main_header+ '

' '
' '

  Introduction  

' '

''

' '
') htmlfile.write(paragraphs) htmlfile.write('' '') print(f.read()) return 0 def pdfhandler(path): f = open(path) print(f.read()) return 0 #Attempting to pass txt file in path path = 'C:/Code/texttohtml/ThisOne.docx' #path = 'C:/Code/texttohtml/memes.txt' extension = path[path.rfind('.')+1: len(path)] filename = path[path.rfind('/')+1: path.rfind('.')] if os.path.exists(filename+".html") == True: os.remove(filename+".html") htmlfile = open(filename+".html", "x") if extension =='txt': txthandler(path, htmlfile) elif extension == 'pdf': pdfhandler(path) elif extension == 'doc' or 'docx': dochandler(path) else: print("Extension not recognized") sys.exit()