简体   繁体   English

在 python 中使用 pandoc 将 Docx 转换为 pdf

[英]Docx to pdf using pandoc in python

So I a quite new to Python so it may be a silly question but i can't seem to find the solution anywhere.所以我对 Python 很陌生,所以这可能是一个愚蠢的问题,但我似乎无法在任何地方找到解决方案。

I have a django site I am running it locally on my machine just for development.我有一个 django 站点,我在我的机器上本地运行它只是为了开发。 on the site I want to convert a docx file to pdf .在网站上,我想将docx文件转换为pdf I want to use pandoc to do this.我想使用pandoc来做到这一点。 I know there are other methods such as online apis or the python modules such as "docx2pdf".我知道还有其他方法,例如在线 apis 或 python 模块,例如“docx2pdf”。 However i want to use pandoc for deployment reasons.但是,出于部署原因,我想使用pandoc

I have installed pandoc on my terminal using brew install pandoc .我已经安装了pandoc使用我的终端上brew install pandoc so it should b installed correctly.所以它应该正确安装。

In my django project i am doing:在我的 Django 项目中,我正在做:

import pypandoc
import docx

def making_a_doc_function(request):
    doc = docx.Document()
    doc.add_heading("MY DOCUMENT")
    doc.save('thisisdoc.docx')
    pypandoc.convert_file('thisisdoc.docx', 'docx', outputfile="thisisdoc.pdf")     
    pdf = open('thisisdoc.pdf', 'rb')
    response = FileResponse(pdf) 
return response

The docx file get created no problem but it not pdf has been created.创建docx文件没问题,但未创建pdf I am getting an error that says:我收到一条错误消息:

Pandoc died with exitcode "4" during conversion: b'cannot produce pdf output from docx\\n'

Does anyone have any ideas?有没有人有任何想法?

The second argument to convert_file is output format, or, in this case, the format through which pandoc generates the pdf. convert_file的第二个参数是输出格式,或者,在这种情况下,pandoc 生成 pdf 的格式。 Pandoc doesn't know how to produce a PDF through docx, hence the error. Pandoc 不知道如何通过 docx 生成 PDF,因此出现错误。

Use pypandoc.convert_file('thisisdoc.docx', 'latex', outputfile="thisisdoc.pdf") or pypandoc.convert_file('thisisdoc.docx', 'pdf', outputfile="thisisdoc.pdf") instead.使用pypandoc.convert_file('thisisdoc.docx', 'latex', outputfile="thisisdoc.pdf")pypandoc.convert_file('thisisdoc.docx', 'pdf', outputfile="thisisdoc.pdf")代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM