linux鐜鍩轰簬python璇█docx杞琾df

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux鐜鍩轰簬python璇█docx杞琾df相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/nic' title='nic'>nic   gen   class   mmu   raise   documents   port   app   imp   

windows骞冲彴鍥犲€熷姪win32com鍏锋湁澶氱鏂规硶灏唚ord杞负pdf锛屼絾linux鐜涓嶅叿澶囨鐜锛寃in32com鍖呬篃灏唅mport澶辫触锛岄偅璇ュ浣曞仛鍛紵

 

# -*- coding: utf-8 -*-
"""
linux platform word to pdf
"""
import subprocess
import os
try:
    from comtypes import client
except ImportError:
    client = None 
try:
    from win32com.client import constants, gencache
except ImportError:
    constants = None
    gencache = None
def doc2pdf_linux(docPath, pdfPath):
    """
    convert a doc/docx document to pdf format (linux only, requires libreoffice)
    :param doc: path to document
    """
    cmd = 鈥榣ibreoffice6.3 --headless --convert-to pdf鈥?split() + [docPath] + [鈥?-outdir鈥榏 + [pdfPath]
    p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    p.wait(timeout=30)
    stdout, stderr = p.communicate()
    if stderr:
        raise subprocess.SubprocessError(stderr)     
def doc2pdf(docPath, pdfPath):
    """
        convert a doc/docx document to pdf format
        :param doc: path to document
        """
    docPathTrue = os.path.abspath(docPath)  # bugfix - searching files in windows/system32
    if client is None:#鍒ゆ柇鐜锛宭inux鐜杩欓噷鑲畾涓篘one
        return doc2pdf_linux(docPathTrue, pdfPath)
    word = gencache.EnsureDispatch(鈥榃ord.Application鈥?
    doc = word.Documents.Open(docPathTrue, ReadOnly=1)
    doc.ExportAsFixedFormat(pdfPath,
                            constants.wdExportFormatPDF,
                            Item=constants.wdExportDocumentWithMarkup,
                            CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
    word.Quit(constants.wdDoNotSaveChanges)
if __name__ == 鈥榑_main__鈥?
    wordpath=鈥?var/db/Report_20191206105753.docx鈥?
    pdfpath=鈥?var/db鈥?
    doc2pdf(wordpath,pdfpath)

銆€銆€

以上是关于linux鐜鍩轰簬python璇█docx杞琾df的主要内容,如果未能解决你的问题,请参考以下文章

鍩轰簬璇嶅吀鍜屾湸绱犺礉鍙舵柉涓枃鎯呮劅鍊惧悜鍒嗘瀽绠楁硶

Spark + Hadoop,鍩轰簬WIFI鎺㈤拡鐨勫ぇ鏁版嵁鍒嗘瀽绯荤粺

NLP-BERT 璋锋瓕鑷劧璇█澶勭悊妯″瀷锛欱ERT-鍩轰簬pytorch

椋炲噷鏂规|鍩轰簬i.MX8MM鐨勬暟瀛桰P缃戠粶骞挎挱绯荤粺

Python-鍩轰簬socket鍜宻elect妯″潡瀹炵幇IO澶氳矾澶嶇敤

璇﹁ВPython涓殑鍚勭杞箟绗n