Python implements the method of converting a doc document to PDF

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.