Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法
更新時間:2015年07月25日 10:15:05 作者:Sephiroth
這篇文章主要介紹了Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法,涉及Python調(diào)用系統(tǒng)win32com組件實(shí)現(xiàn)文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
import sys, os
from win32com.client import Dispatch, constants, gencache
def 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, ReadOnly = 1)
doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,
Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks)
return 0
except:
return 1
finally:
w.Quit(constants.wdDoNotSaveChanges)
# 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.argv[1]
output = os.path.splitext(input)[0]+'.pdf'
elif (len(sys.argv) == 3):
input = sys.argv[1]
output = sys.argv[2]
else:
usage()
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
except:
return -1
if __name__=='__main__':
rc = main()
if rc:
sys.exit(rc)
sys.exit(0)
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
python client使用http post 到server端的代碼
python client使用 http post 到server端的代碼,供大家學(xué)習(xí)參考2013-02-02
Python opencv相機(jī)標(biāo)定實(shí)現(xiàn)原理及步驟詳解
這篇文章主要介紹了Python opencv相機(jī)標(biāo)定實(shí)現(xiàn)原理及步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
echarts折線圖的每個折點(diǎn)都顯示數(shù)值的實(shí)現(xiàn)方式
這篇文章主要介紹了echarts折線圖的每個折點(diǎn)都顯示數(shù)值的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Python3實(shí)現(xiàn)配置文件差異對比腳本
這篇文章主要介紹了Python3實(shí)現(xiàn)配置文件差異對比腳本,本文通過案例場景分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11

