日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Python 中docx转pdf

發布時間:2023/12/13 综合教程 19 生活家
生活随笔 收集整理的這篇文章主要介紹了 Python 中docx转pdf 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#第一種
import comtypes.client
def convertDocxToPDF(infile,outfile):
wdFormatPDF = 17
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(infile)
doc.SaveAs(outfile, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()

#第二種
from win32com.client import Dispatch, constants, 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,
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 trsfer():
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'' # word文檔的名稱(可帶地址)
output = u'' # 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
except:
return -1

if __name__ == '__main__':
res=trsfer()
if rc:
sys.exit(rc)
sys.exit(0)
---------------------
作者:qq_40219176
來源:CSDN
原文:https://blog.csdn.net/qq_40219176/article/details/78599980
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

總結

以上是生活随笔為你收集整理的Python 中docx转pdf的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。