├── office2pdf.py └── readme.txt /office2pdf.py: -------------------------------------------------------------------------------- 1 | ################################################################# 2 | # Requires Office 2007 SP2 or higher 3 | # Requires python for win32 extension 4 | # 5 | # Author: Ma Liang 6 | # Date: 2012-05-06 7 | # Version: 1.0 8 | # 9 | ################################################################# 10 | 11 | import sys, os 12 | from win32com.client import Dispatch, constants, gencache 13 | def usage(): 14 | sys.stderr.write ("doc2pdf.py input [output]") 15 | sys.exit(2) 16 | 17 | #powerpoint convert pdf file 18 | def ppt2pdf(input,output): 19 | p = Dispatch("PowerPoint.Application") 20 | p.Visible = True 21 | try: 22 | print input 23 | pptFile = p.Presentations.Open(input) 24 | print 'open ppt successful!' 25 | pptFile.SaveAs(output) 26 | #pptFile.ExportAsFixedFormat(output,17) 27 | print 'powerpoint file convert successful!' 28 | return 0 29 | except: 30 | print 'powerpoint file convert failed!' 31 | return 1 32 | finally: 33 | p.Quit() 34 | 35 | #word convert pdf file 36 | def doc2pdf(input, output): 37 | w = Dispatch("Word.Application") 38 | try: 39 | doc = w.Documents.Open(input, ReadOnly = 1) 40 | doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, 41 | Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks) 42 | print 'word file convert successful!' 43 | return 0 44 | except: 45 | print 'word file convert failed!' 46 | return 1 47 | finally: 48 | w.Quit(constants.wdDoNotSaveChanges) 49 | #excel convert pdf file 50 | def excel2pdf(input, output): 51 | excelSource = Dispatch("Excel.Application") 52 | try: 53 | excel = excelSource.Workbooks.Open(input,ReadOnly = 1) 54 | xlTypePDF = 0; 55 | xlQualityStandard = 0; 56 | excel.ExportAsFixedFormat(xlTypePDF,output) 57 | print 'excel file convert successful!' 58 | return 0 59 | except: 60 | print 'excel file convert failed!' 61 | return 1 62 | finally: 63 | excel.Quit() 64 | 65 | # Generate all the support we can. 66 | def GenerateSupport(): 67 | # enable python COM support for Word 2007 68 | # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library" 69 | gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) 70 | 71 | def main(): 72 | if (len(sys.argv) == 2): 73 | input = sys.argv[1] 74 | output = os.path.splitext(input)[0]+'.pdf' 75 | elif (len(sys.argv) == 3): 76 | input = sys.argv[1] 77 | output = sys.argv[2] 78 | else: 79 | usage() 80 | if (not os.path.isabs(input)): 81 | input = os.path.abspath(input) 82 | if (not os.path.isabs(output)): 83 | output = os.path.abspath(output) 84 | try: 85 | GenerateSupport() 86 | shortLastName = input[-3:] 87 | longLastName = input[-4:] 88 | if shortLastName == 'xls' or longLastName == 'xlsx': 89 | rc = excel2pdf(input, output) 90 | elif shortLastName == 'doc' or longLastName == 'docx': 91 | rc = doc2pdf(input,output) 92 | elif shortLastName == 'ppt' or longLastName == 'pptx': 93 | rc = ppt2pdf(input,output) 94 | return rc 95 | except: 96 | return -1 97 | 98 | if __name__=='__main__': 99 | rc = main() 100 | if rc: 101 | sys.exit(rc) 102 | sys.exit(0) 103 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | office2pdf 2 | --------------------------------------------------------------------------------