Use Python to convert Word 2007 's documents to PDF files __python

Source: Internet
Author: User
Tags python script

During development, you encounter the requirement to convert a doc document (or other Office document) to PDF under the command line. For example, if the manual is in doc format in the project, you want to convert it to PDF format when the project is published, and keep the bookmarks, links, etc. in Doc. Consolidating the process into the build process requires that the conversion be done under the command line.
Michael Suodenjoki shows the use of Office COM interfaces for conversion under the command line. However, the exported PDF document has no bookmarks. In Office 2007 SP2, Microsoft has added this feature, and the corresponding interface is ExportAsFixedFormat. This method applies not only to word, but also to Excel. The
face is a simple Python script to show how to convert Doc to PDF. The script requires Office 2007 SP2, Python 2.6, and Python for Win32 (enabling Python to invoke COM). Other languages that support COM can also be used here. ExportAsFixedFormat also has some other parameters, as detailed in the MSDN related documentation. Note that the document path needs to be an absolute path because the current path after Word starts is not the current path when the script is invoked.

#-*-coding:utf-8-*-# Doc2pdf.py:python script to convert doc to PDF with bookmarks! # Requires Office 2007 SP2 # Requires python for Win32 extension import SYS, OS from win32com.client import Dispatch, CO Nstants, Gencache def doc2pdf (input, Output): W = Dispatch ("Word.Application") Try:doc = W.documents.open (input , ReadOnly = 1) doc. ExportAsFixedFormat (output, constants.wdexportformatpdf, Item = Constants.wdexportdocumentwithmarkup, CreateBookmar KS = 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 2007 # This is generated 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.ar GV[1] Output = os.path.splitext (input) [0]+ '. pdf ' Elif (len (SYS.ARGV) = = 3): input = sys.argv[1] output = sys.argv[2] else:input = U ' ba06007013.docx ' #word文档的名称 output = U ' B A06007013.pdf ' #pdf文档的名称 if (not 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 Exce
 pt:return-1 if __name__== ' __main__ ': rc = Main () if Rc:sys.exit (RC) sys.exit (0)


Try, really good, better than adobe software effect!

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.