def __init__(self): self.__app = Word.ApplicationClass() self.__missing = System.Type.Missing self.__pdfFormat = Word.WdExportFormat.wdExportFormatPDF self.__optimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint self.__rangeAll = Word.WdExportRange.wdExportAllDocument self.__itemDocContent = Word.WdExportItem.wdExportDocumentContent self.__createBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks self.__doc = None self.__file = None self.__extMap = { ".txt": Word.WdSaveFormat.wdFormatText, ".utxt": Word.WdSaveFormat.wdFormatUnicodeText, ".odt": Word.WdSaveFormat.wdFormatOpenDocumentText, ".doc": Word.WdSaveFormat.wdFormatDocument, ".docx": Word.WdSaveFormat.wdFormatXMLDocument, ".html": Word.WdSaveFormat.wdFormatFilteredHTML, ".htm": Word.WdSaveFormat.wdFormatFilteredHTML, ".fullhtml": Word.WdSaveFormat.wdFormatHTML, ".fxml": Word.WdSaveFormat.wdFormatFlatXML, ".webarc": Word.WdSaveFormat.wdFormatWebArchive, ".dos": Word.WdSaveFormat.wdFormatDOSText, ".rdf": Word.WdSaveFormat.wdFormatRTF, ".pdf": None }
def doc_replace_text(source_filename, tokens, values): global errors word_application = Word.ApplicationClass() word_application.visible = True document = word_application.Documents.Open(source_filename) #Find and Replace Process for _find, _replace in zip(tokens, values): for myStoryRange in document.StoryRanges: find_replace(myStoryRange, _find, _replace) try: while myStoryRange.NextStoryRange is not None: q = myStoryRange.NextStoryRange find_replace(q, _find, _replace) except: import traceback errors.append(traceback.format_exc()) #Find and replace in TextBox(shapes) try: for shape in document.Shapes: initialText = shape.TextFrame if initialText.HasText: rangeobj = initialText.TextRange find_replace(rangeobj, _find, _replace) except: import traceback errors.append(traceback.format_exc())
def __init__(self): self.error = [] try: self.interrop = System.Runtime.InteropServices.Marshal.GetActiveObject( "Word.Application") self.wapp = self.interrop.Application self.document = self.wapp.ActiveDocument self.docName = self.document.FullName except Exception as ex: self.error.append(ex) self.wapp = Word.ApplicationClass() self.document = self.wapp.ActiveDocument self.docName = self.document.FullName
def doc_to_text(filename): word_application = Word.ApplicationClass() word_application.visible = False document = word_application.Documents.Open(filename) result = StringBuilder() for p in document.Paragraphs: result.Append(clean_text(p.Range.Text)) document.Close() document = None word_application.Quit() word_application = None return result.ToString()
def Run(scriptName): word_application = Word.ApplicationClass() word_application.visible = False sleep(1) source = r"c:\ddd\secondscript.docx" destination = r"c:\ddd\secondscript_new.docx" document = word_application.Documents.Open(source) print "Updating Word Document" range = word_application.ActiveDocument.Range(0, 5) range.Text = "Hey from script" document.SaveAs(destination) document.Close() document = None print "Saved Word Document to " + destination word_application.Quit() word_application = None sleep(1) # wait to make sure we're detached from process
def doc_replace_text(source_filename, tokens, values): try: missing = System.Type.Missing replaceAll = Word.WdReplace.wdReplaceAll word_application = Word.ApplicationClass() word_application.visible = False document = word_application.Documents.Open(source_filename) #Find and Replace Process for _find, _replace in zip(tokens, values): for myStoryRange in document.StoryRanges: myStoryRange.Find.Execute( _find, # search text True, # match case missing, # match whole word missing, # match wildcards missing, # match sounds like missing, # match all word forms True, # forward? Word.WdFindWrap.wdFindContinue, # wrap enum missing, # format? _replace, # replace with replaceAll, # replace enum missing, # match Kashida (arabic letters) missing, # diatrics? missing, # match other Arabic letters missing # support right to left ) try: while myStoryRange.NextStoryRange is not None: q = myStoryRange.NextStoryRange q.Find.Execute( _find, # search text True, # match case missing, # match whole word missing, # match wildcards missing, # match sounds like missing, # match all word forms True, # forward? Word.WdFindWrap.wdFindContinue, # wrap enum missing, # format? _replace, # replace with replaceAll, # replace enum missing, # match Kashida (arabic letters) missing, # diatrics? missing, # match other Arabic letters missing # support right to left ) except: pass #Find and replace in TextBox(shapes) try: for shape in document.Shapes: initialText = shape.TextFrame if initialText.HasText and _find == "PHASE EXE": rangeobj = initialText.TextRange rangeobj.Find.Execute( _find, # search text True, # match case missing, # match whole word missing, # match wildcards missing, # match sounds like missing, # match all word forms True, # forward? Word.WdFindWrap.wdFindContinue, # wrap enum missing, # format? _replace, # replace with replaceAll, # replace enum missing, # match Kashida (arabic letters) missing, # diatrics? missing, # match other Arabic letters missing # support right to left ) #OTHER METHOD #resultingText = initialText.TextRange.Text.Replace(_find, _replace) #shape.TextFrame.TextRange.Text = resultingText except: pass print source_filename + ": success" pdf_path = source_filename + ".pdf" document.Save() document.SaveAs2(pdf_path, Word.WdSaveFormat.wdFormatPDF) document.Close() document = None word_application.Quit() word_application = None #Marshal.ReleaseComObject(app) except: # if error occurs anywhere in the process catch it import traceback errorReport = traceback.format_exc() print errorReport document.Close() document = None word_application.Quit() word_application = None
document.Close() document = None word_application.Quit() word_application = None #Marshal.ReleaseComObject(app) except: # if error occurs anywhere in the process catch it import traceback errorReport = traceback.format_exc() print errorReport document.Close() document = None word_application.Quit() word_application = None tokens = ["EXE", "PHASE EXE"] # etc... values = ["DOE", "PHASE DOE"] #doc_replace_text(source_filename, tokens, values, destination_filename) word_application = Word.ApplicationClass() word_application.Quit() word_application = None monRepertoire = "C:\\Users\\userName\\Documents\\Proposées" for (repertoire, sousRepertoires, fichiers) in walk(monRepertoire): for file in fichiers: if file.endswith(".docx"): filename = (os.path.join(repertoire, file)) doc_replace_text(filename, tokens, values)