直接上代码
# -*- encoding:utf-8 -*- """ author:lgh 简单的doc转pdf,html,pdf转doc脚本 依赖库pdfminer3k,pip install pdfminer3k即可 """ from win32com.client import Dispatch, constants from pdfminer.pdfparser import PDFParser, PDFDocument from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.layout import LAParams, LTTextBoxHorizontal from pdfminer.converter import PDFPageAggregator from pdfminer.pdfinterp import PDFTextExtractionNotAllowed def doc2pdfinput, output): w = Dispatch'Word.Application') try: # 打开文件 doc = w.Documents.Openinput, ReadOnly=1) # 转换文件 doc.ExportAsFixedFormatoutput, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks) return True except Exception as e: printe) return False finally: w.Quitconstants.wdDoNotSaveChanges) def doc2htmlinput, output): w = Dispatch'Word.Application') try: doc = w.Documents.Openinput, ReadOnly=1) doc.SaveAsoutput, 8) return True except Exception as e: printe) return False finally: w.Quitconstants.wdDoNotSaveChanges) def pdf2docinput, output): try: with openinput, 'rb') as f: parser = PDFParserf) doc = PDFDocument) parser.set_documentdoc) doc.set_parserparser) # 设置初始化密码 doc.initialize) if not doc.is_extractable: raise PDFTextExtractionNotAllowed else: rsrcmgr = PDFResourceManager) laparams = LAParams) device = PDFPageAggregatorrsrcmgr, laparams=laparams) interpreter = PDFPageInterpreterrsrcmgr, device) for page in doc.get_pages): interpreter.process_pagepage) layout = device.get_result) for x in layout: if isinstancex, LTTextBoxHorizontal): with openoutput, 'a', encoding='utf-8') as f1: results = x.get_text) f1.writeresults+' ') return True except Exception as e: printe) return False def main): # rc = doc2pdfinput, output) # rc = doc2htmlinput, output) input = r'F:save_data流畅的Python.pdf' output = r'F:save_data est.doc' rc = pdf2docinput, output) if rc: print'转换成功') else: print'转换失败') if __name__ == '__main__': main)
以上其实是通过com来调用office API,其他语言貌似也可以
当然你也可以用上面的代码将word文件转换成任意格式文件(只要office 2007支持,比如将word文件转换成PDF文件,把8改成17即可),下面是office 2007支持的全部文件格式对应表:
wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
wdFormatXMLDocumentMacroEnabled = 13
wdFormatXMLTemplate = 14
wdFormatXMLTemplateMacroEnabled = 15
wdFormatXPS = 18照着字面意思应该能对应到相应的文件格式,如果你是office 2003可能支持不了这么多格式。word文件转html有两种格式可选wdFormatHTML、wdFormatFilteredHTML(对应数字8、10),区别是如果是wdFormatHTML格式的话,word文件里面的公式等ole对象将会存储成wmf格式,而选用wdFormatFilteredHTML的话公式图片将存储为gif格式,而且目测可以看出用wdFormatFilteredHTML生成的HTML明显比wdFormatHTML要干净许多。
参考自https://blog.csdn.net/binger819623/article/details/6770932