使用纯 python 将 docx 转换为 pdf(在 linux 上,没有 libreoffice)
Converting docx to pdf with pure python (on linux, without libreoffice)
我正在处理一个尝试开发网络应用程序的问题,其中一部分将上传的 docx 文件转换为 pdf 文件(经过一些处理)。使用 python-docx
和其他方法,对于大多数处理(我的网络服务器是 pythonanywhere - linux 但没有 libreoffice 并且没有 sudo
或 apt install
权限)。但是转换为 pdf 似乎需要其中之一。通过探索这里和其他地方的问题,这是我目前所知道的:
import subprocess
try:
from comtypes import client
except ImportError:
client = None
def doc2pdf(doc):
"""
convert a doc/docx document to pdf format
:param doc: path to document
"""
doc = os.path.abspath(doc) # bugfix - searching files in windows/system32
if client is None:
return doc2pdf_linux(doc)
name, ext = os.path.splitext(doc)
try:
word = client.CreateObject('Word.Application')
worddoc = word.Documents.Open(doc)
worddoc.SaveAs(name + '.pdf', FileFormat=17)
except Exception:
raise
finally:
worddoc.Close()
word.Quit()
def doc2pdf_linux(doc):
"""
convert a doc/docx document to pdf format (linux only, requires libreoffice)
:param doc: path to document
"""
cmd = 'libreoffice --convert-to pdf'.split() + [doc]
p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p.wait(timeout=10)
stdout, stderr = p.communicate()
if stderr:
raise subprocess.SubprocessError(stderr)
如您所见,一种方法需要 comtypes
,另一种方法需要 libreoffice
作为子流程。除了切换到更复杂的托管服务器之外,还有什么解决办法吗?
PythonAnywhere 帮助页面在此处提供有关处理 PDF 文件的信息:https://help.pythonanywhere.com/pages/PDF
总结:PythonAnywhere 安装了许多 Python PDF 操作包,其中之一可能会满足您的需求。但是,shell 达到 abiword
对我来说似乎是最简单的。 shell 命令 abiword --to=pdf filetoconvert.docx
会将 docx 文件转换为 PDF 并在与 docx 相同的目录中生成一个名为 filetoconvert.pdf
的文件。请注意,此命令将向标准错误流输出一条错误消息,抱怨 XDG_RUNTIME_DIR
(或者至少它对我如此),但它仍然有效,并且可以忽略该错误消息。
您可以使用的另一个是 libreoffice,但是正如第一响应者所说,质量永远不会像使用实际的 comtypes 那样好。
无论如何,在您安装了 libreoffice 之后,这里是执行此操作的代码。
from subprocess import Popen
LIBRE_OFFICE = r"C:\Program Files\LibreOffice\program\soffice.exe"
def convert_to_pdf(input_docx, out_folder):
p = Popen([LIBRE_OFFICE, '--headless', '--convert-to', 'pdf', '--outdir',
out_folder, input_docx])
print([LIBRE_OFFICE, '--convert-to', 'pdf', input_docx])
p.communicate()
sample_doc = 'file.docx'
out_folder = 'some_folder'
convert_to_pdf(sample_doc, out_folder)
我在 Linux 环境中找到了最简单的方法...
进口os
os.system("lowriter --convert-to pdf" +str(" ") + str(file_path))
这是 linux 的 docx 到 pdf 代码(对于 windows 只需下载 libreoffice 并放置 soffice 路径而不是 soffice)
import subprocess
def generate_pdf(doc_path, path):
subprocess.call(['soffice',
# '--headless',
'--convert-to',
'pdf',
'--outdir',
path,
doc_path])
return doc_path
generate_pdf("docx_path.docx", "output_path")
我正在处理一个尝试开发网络应用程序的问题,其中一部分将上传的 docx 文件转换为 pdf 文件(经过一些处理)。使用 python-docx
和其他方法,对于大多数处理(我的网络服务器是 pythonanywhere - linux 但没有 libreoffice 并且没有 sudo
或 apt install
权限)。但是转换为 pdf 似乎需要其中之一。通过探索这里和其他地方的问题,这是我目前所知道的:
import subprocess
try:
from comtypes import client
except ImportError:
client = None
def doc2pdf(doc):
"""
convert a doc/docx document to pdf format
:param doc: path to document
"""
doc = os.path.abspath(doc) # bugfix - searching files in windows/system32
if client is None:
return doc2pdf_linux(doc)
name, ext = os.path.splitext(doc)
try:
word = client.CreateObject('Word.Application')
worddoc = word.Documents.Open(doc)
worddoc.SaveAs(name + '.pdf', FileFormat=17)
except Exception:
raise
finally:
worddoc.Close()
word.Quit()
def doc2pdf_linux(doc):
"""
convert a doc/docx document to pdf format (linux only, requires libreoffice)
:param doc: path to document
"""
cmd = 'libreoffice --convert-to pdf'.split() + [doc]
p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p.wait(timeout=10)
stdout, stderr = p.communicate()
if stderr:
raise subprocess.SubprocessError(stderr)
如您所见,一种方法需要 comtypes
,另一种方法需要 libreoffice
作为子流程。除了切换到更复杂的托管服务器之外,还有什么解决办法吗?
PythonAnywhere 帮助页面在此处提供有关处理 PDF 文件的信息:https://help.pythonanywhere.com/pages/PDF
总结:PythonAnywhere 安装了许多 Python PDF 操作包,其中之一可能会满足您的需求。但是,shell 达到 abiword
对我来说似乎是最简单的。 shell 命令 abiword --to=pdf filetoconvert.docx
会将 docx 文件转换为 PDF 并在与 docx 相同的目录中生成一个名为 filetoconvert.pdf
的文件。请注意,此命令将向标准错误流输出一条错误消息,抱怨 XDG_RUNTIME_DIR
(或者至少它对我如此),但它仍然有效,并且可以忽略该错误消息。
您可以使用的另一个是 libreoffice,但是正如第一响应者所说,质量永远不会像使用实际的 comtypes 那样好。
无论如何,在您安装了 libreoffice 之后,这里是执行此操作的代码。
from subprocess import Popen
LIBRE_OFFICE = r"C:\Program Files\LibreOffice\program\soffice.exe"
def convert_to_pdf(input_docx, out_folder):
p = Popen([LIBRE_OFFICE, '--headless', '--convert-to', 'pdf', '--outdir',
out_folder, input_docx])
print([LIBRE_OFFICE, '--convert-to', 'pdf', input_docx])
p.communicate()
sample_doc = 'file.docx'
out_folder = 'some_folder'
convert_to_pdf(sample_doc, out_folder)
我在 Linux 环境中找到了最简单的方法...
进口os
os.system("lowriter --convert-to pdf" +str(" ") + str(file_path))
这是 linux 的 docx 到 pdf 代码(对于 windows 只需下载 libreoffice 并放置 soffice 路径而不是 soffice)
import subprocess
def generate_pdf(doc_path, path):
subprocess.call(['soffice',
# '--headless',
'--convert-to',
'pdf',
'--outdir',
path,
doc_path])
return doc_path
generate_pdf("docx_path.docx", "output_path")