Import win32com From win32com. client import Dispatch, constants W = win32com. client. Dispatch ('word. application ') # Use the following method to start an independent process: # W = win32com. client. DispatchEx ('word. application ') # Running in the background, not displayed, not warned W. Visible = 0 W. DisplayAlerts = 0 # Open a new file Doc = w. Documents. Open (FileName = filenamein) # Worddoc = w. Documents. Add () # create a new document # Insert text MyRange = doc. Range (0, 0) MyRange. InsertBefore ('Hello from Python! ') # Use Style WordSel = myRange. Select () WordSel. Style = constants. wdStyleHeading1 # Text replacement W. Selection. Find. ClearFormatting () W. Selection. Find. Replacement. ClearFormatting () W. Selection. Find. Execute (OldStr, False, True, 1, True, NewStr, 2) # Header text replacement W. ActiveDocument. Sections [0]. Headers [0]. Range. Find. ClearFormatting () W. ActiveDocument. Sections [0]. Headers [0]. Range. Find. Replacement. ClearFormatting () W. ActiveDocument. Sections [0]. Headers [0]. Range. Find. Execute (OldStr, False, True, 1, False, NewStr, 2) # Table operations Doc. Tables [0]. Rows [0]. Cells [0]. Range. Text = '20140901' Worddoc. Tables [0]. Rows. Add () # Add a row # Converting to html Wc = win32com. client. constants W. ActiveDocument. WebOptions. RelyOnCSS = 1 W. ActiveDocument. WebOptions. OptimizeForBrowser = 1 W. ActiveDocument. WebOptions. BrowserLevel = 0 # constants. wdBrowserLevelV4 W. ActiveDocument. WebOptions. OrganizeInFolder = 0 W. ActiveDocument. WebOptions. UseLongFileNames = 1 W. ActiveDocument. WebOptions. RelyOnVML = 0 W. ActiveDocument. WebOptions. AllowPNG = 1 W. ActiveDocument. SaveAs (FileName = filenameout, FileFormat = wc. wdFormatHTML) # Print Doc. PrintOut () # Disable # Doc. Close () W. Documents. Close (wc. wdDoNotSaveChanges) W. Quit () |