77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
|
"""
|
|||
|
You need to install these libraries in the Linux os.
|
|||
|
1、sudo apt-get install python3
|
|||
|
2、sudo apt-get install libreoffice
|
|||
|
3、sudo apt-get install unoconv
|
|||
|
4、cd /usr/share/fonts/ mkdir chinese
|
|||
|
5、cp Windows os C:\Windows\Fonts to /usr/share/fonts/chinese
|
|||
|
chmod -R 755 /usr/share/fonts/chinese
|
|||
|
fc-cache -fv
|
|||
|
fc-list | grep chinese //check the new fonts
|
|||
|
6、Directions for use
|
|||
|
For example:
|
|||
|
python3 doc2pdf.py "./MI_SYS_API_V3.docx" "./MI_SYS_API_V3.pdf"
|
|||
|
"""
|
|||
|
import subprocess
|
|||
|
import os
|
|||
|
import sys
|
|||
|
|
|||
|
try:
|
|||
|
from comtypes import client
|
|||
|
except ImportError:
|
|||
|
client = None
|
|||
|
|
|||
|
try:
|
|||
|
from win32com.client import constants, gencache
|
|||
|
except ImportError:
|
|||
|
constants = None
|
|||
|
gencache = None
|
|||
|
|
|||
|
|
|||
|
def doc2pdf(docPath, pdfPath):
|
|||
|
"""
|
|||
|
convert a doc/docx document to pdf format
|
|||
|
:docPath: path to doc/docx Input
|
|||
|
:pdfPath: path to pdf Output
|
|||
|
"""
|
|||
|
docPathTrue = os.path.abspath(docPath)
|
|||
|
if client is None:#判断环境,linux环境这里肯定为None
|
|||
|
return doc2pdf_linux(docPathTrue, pdfPath)
|
|||
|
# name, ext = os.path.splitext(doc)
|
|||
|
# try:
|
|||
|
# word = client.DispatchEx("Word.Application")
|
|||
|
# worddoc = word.Documents.Open(doc)
|
|||
|
# worddoc.SaveAs(name + '.pdf', FileFormat=17)
|
|||
|
# except Exception:
|
|||
|
# raise
|
|||
|
# finally:
|
|||
|
# worddoc.Close()
|
|||
|
# word.Quit()
|
|||
|
word = gencache.EnsureDispatch('Word.Application')
|
|||
|
doc = word.Documents.Open(docPathTrue, ReadOnly=1)
|
|||
|
doc.ExportAsFixedFormat(pdfPath,
|
|||
|
constants.wdExportFormatPDF,
|
|||
|
Item=constants.wdExportDocumentWithMarkup,
|
|||
|
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
|
|||
|
word.Quit(constants.wdDoNotSaveChanges)
|
|||
|
|
|||
|
|
|||
|
def doc2pdf_linux(docPath, pdfPath):
|
|||
|
"""
|
|||
|
convert a doc/docx document to pdf format (linux only, requires libreoffice)
|
|||
|
:param doc: path to doc/docx document
|
|||
|
cmd = 'libreoffice --headless --convert-to pdf :writer_pdf_Export'.split() + [filePath] + ['--outdir'] + [destDir]
|
|||
|
"""
|
|||
|
filePath = docPath
|
|||
|
destDir = pdfPath
|
|||
|
cmd = 'libreoffice --headless --convert-to pdf :writer_pdf_Export'.split() + [filePath] + ['--outdir'] + [destDir]
|
|||
|
print(filePath)
|
|||
|
p = subprocess.Popen(cmd,stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
|||
|
p.wait(timeout=60)
|
|||
|
stdout, stderr = p.communicate()
|
|||
|
if stderr:
|
|||
|
raise subprocess.SubprocessError(stderr)
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
doc2pdf(sys.argv[1], sys.argv[2])
|