The example in this article describes how Python implements the conversion of DOC documents to PDFs. Share to everyone for your reference. The implementation method is as follows:
Import sys, osfrom win32com.client import Dispatch, constants, Gencachedef usage (): sys.stderr.write ("doc2pdf.py input [ Output] ") Sys.exit (2) def doc2pdf (input, Output): W = Dispatch (" Word.Application ") Try:doc = W.documents.open (input, Rea DOnly = 1) doc. ExportAsFixedFormat (output, constants.wdexportformatpdf, Item = Constants.wdexportdocumentwithmarkup, Createbookmarks = Constants.wdexportcreateheadingbookmarks) return 0 Except:return 1 finally:w.quit (constants.wdDoNot SaveChanges) # Generate All the "Support we Can.def Generatesupport (): # Enable Python COM support for Word # this is G enerated by:makepy.py-i "Microsoft Word 12.0 Object Library" Gencache. Ensuremodule (' {00020905-0000-0000-c000-000000000046} ', 0, 8, 4) def main (): if (len (sys.argv) = = 2): input = sys.argv[1] Output = Os.path.splitext (input) [0]+ '. pdf ' Elif (len (sys.argv) = = 3): input = sys.argv[1] output = sys.argv[2] Else:us Age () if (no os.path.isabs (input)): input = Os.path.abspath (input) if (not Os.path.isabs (output)): output = Os.path.abspath (output) try:generatesupport () rc = doc2pdf (input, output) return RC Except:return-1if __name__== ' __main__ ': rc = Main () if Rc:sys.exit (RC) sys.exit (0)
Hopefully this article will help you with Python programming.