使用python將word轉為pdf


這種方法,依賴的是window環境,在其他環境中使用會出現問題
from win32com.client import constants, gencache
def createPdf(wordPath, pdfPath):
    """
    word轉pdf
    :param wordPath: word文件路徑
    :param pdfPath:  生成pdf文件路徑
    """
    word = gencache.EnsureDispatch('Word.Application')
    doc = word.Documents.Open(wordPath, ReadOnly=1)
    doc.ExportAsFixedFormat(pdfPath,
                            constants.wdExportFormatPDF,
                            Item=constants.wdExportDocumentWithMarkup,
                            CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
    word.Quit(constants.wdDoNotSaveChanges)
 本以為通過上面的方法將word轉pdf,可以實現需求,奈何上傳服務之后,使用了linux系統,這種方法就不能夠使用。經過多方尋找,只要我們服務器上面的libreoffice安裝好,且環境頁配置完成,就可以使用如下方法,來實現word轉pdf:
import os
os.system("libreoffice6.4 --headless --convert-to pdf --outdir /root/Conv/ /root/TEST.docx")

  以上兩種方法,可以分別實現window系統和linux系統中word轉pdf。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM