Python code to convert .docx , .doc and .pdf files to .txt files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | from os import chdir, getcwd, listdir, path from time import strftime from win32com import client import os import pyPdf def count_files(filetype): count_files = 0 for files in listdir(folder): if files.endswith(filetype): count_files += 1 return count_files def check_path(prompt): ''' (str) -> str Verifies if the provided absolute path does exist. ''' abs_path = raw_input(prompt) while path.exists(abs_path) != True: print "\nThe specified path does not exist.\n" abs_path = raw_input(prompt) return abs_path print "\n" folder = check_path("Provide absolute path for the folder: ") chdir(folder) num_docx = count_files(".docx") num_doc = count_files(".doc") num_pdf=count_files(".pdf") if num_docx + num_doc +num_pdf == 0: print "\nThe specified folder does not contain docx,doc or pdf files.\n" print strftime("%H:%M:%S"), "There are no files to convert. BYE, BYE!." exit() else: print "\nNumber of doc,docx and pdf files: ", num_docx + num_doc +num_pdf, "\n" print strftime("%H:%M:%S"), "Starting to convert files ...\n" try: word = client.DispatchEx("Word.Application") for files in listdir(getcwd()): if files.endswith(".docx"): new_name = files.replace(".docx", r".txt") in_file = path.abspath(folder + "\\" + files) new_file = path.abspath(folder + "\\" + new_name) doc = word.Documents.Open(in_file) print strftime("%H:%M:%S"), " docx -> txt ", path.relpath(new_file) doc.SaveAs(new_file, FileFormat = 2) doc.Close() if files.endswith(".doc"): new_name = files.replace(".doc", r".txt") in_file = path.abspath(folder + "\\" + files) new_file = path.abspath(folder + "\\" + new_name) doc = word.Documents.Open(in_file) print strftime("%H:%M:%S"), " doc -> txt ", path.relpath(new_file) doc.SaveAs(new_file, FileFormat = 2) doc.Close() except Exception, e: print e finally: word.Quit() list=[] directory=folder for root,dirs,files in os.walk(directory): for filename in files: if filename.endswith('.pdf'): t=os.path.join(directory,filename) list.append(t) m=len(list) i=0 while i<=len(list): path=list[i] head,tail=os.path.split(path) var="\\" tail=tail.replace(".pdf",".txt") name=head+var+tail content = "" # Load PDF into pyPDF pdf = pyPdf.PdfFileReader(file(path, "rb")) # Iterate pages for i in range(0, pdf.getNumPages()): # Extract text from page and add to content content += pdf.getPage(i).extractText() + "\n" print strftime("%H:%M:%S"), " pdf -> txt " f=open(name,'w') f.write(content.encode("UTF-8")) f.close print "\n", strftime("%H:%M:%S"), "Finished converting .doc .docx and .pdf files." # Count the number of txt files. num_txt = count_files(".txt") print "\nNumber of txt files: ", num_txt |