Python实现将doc转化pdf格式文档的方法
本文实例讲述了Python实现将doc转化pdf格式文档的方法。分享给大家供大家参考,具体如下:
#-*-coding:utf-8-*-
#doc2pdf.py:pythonscripttoconvertdoctopdfwithbookmarks!
#RequiresOffice2007SP2
#Requirespythonforwin32extension
importsys,os
fromwin32com.clientimportDispatch,constants,gencache
defdoc2pdf(input,output):
w=Dispatch("Word.Application")
try:
doc=w.Documents.Open(input,ReadOnly=1)
doc.ExportAsFixedFormat(output,constants.wdExportFormatPDF,\
Item=constants.wdExportDocumentWithMarkup,CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
return0
except:
return1
finally:
w.Quit(constants.wdDoNotSaveChanges)
#Generateallthesupportwecan.
defGenerateSupport():
#enablepythonCOMsupportforWord2007
#thisisgeneratedby:makepy.py-i"MicrosoftWord12.0ObjectLibrary"
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}',0,8,4)
defmain():
print(len(sys.argv))
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:
input=u'BA06007013.docx'#word文档的名称
output=u'BA06007013.pdf'#pdf文档的名称
if(notos.path.isabs(input)):
input=os.path.abspath(input)
if(notos.path.isabs(output)):
output=os.path.abspath(output)
try:
GenerateSupport()
rc=doc2pdf(input,output)
returnrc
except:
return-1
if__name__=='__main__':
print("hello")
rc=main()
ifrc:
sys.exit(rc)
sys.exit(0)
php调用py程序
项目查重检测系统 html{font-size:16px;} fieldset{width:1080px;margin:0auto;} legend{font-weight:bold;font-size:14px;} label{float:left;width:120px;margin-left:10px;} .left{margin-left:120px;} .input{width:150px;} span{color:#666666;} 热门推荐