繁体   English   中英

使用 pandoc 将 docx 转换为 pdf 时出错

[英]error converting docx to pdf using pandoc

来自关于如何在 r 中将 docx 转换为 PDF的讨论 ,我尝试使用以下代码将 .docx 转换为 pdf。

pandoc <- "C:/Users/.../Pandoc/pandoc.exe"
input <- "C:/Users/.../abc.docx"
output <- "C:/Users/.../abc.pdf"
cmd <- sprintf('"%s" "%s" -o "%s"', pandoc, input, output)
shell(cmd)

但是,我收到“ execution failed with error code 1 ”错误。 解决办法是什么? 如果在 R 中运行此程序时出现问题,我该如何使用其他工具执行此操作?

我一直在用这种方法遇到同样的问题——我就是无法让它工作。

但是,我确实想出了一种使用 RDCOMClient 将 docx 转换为 PDF 的方法。

library(RDCOMClient)

file <- "C:/path/to your/doc.docx"

wordApp <- COMCreate("Word.Application")  # create COM object
wordApp[["Visible"]] <- TRUE #opens a Word application instance visibly
wordApp[["Documents"]]$Add() #adds new blank docx in your application
wordApp[["Documents"]]$Open(Filename=file) #opens your docx in wordApp

#THIS IS THE MAGIC    
wordApp[["ActiveDocument"]]$SaveAs("C:/path/to your/new.pdf", 
FileFormat=17) #FileFormat=17 saves as .PDF

wordApp$Quit() #quit wordApp

我在这里找到了 FileFormat=17 位https://docs.microsoft.com/en-us/office/vba/api/word.wdexportformat

编辑:替代选项 - 通过 Reticulate 包在 R 中使用 Python。 这使用 pywin32 Python 包。 如果你没有它,你可以使用这里的说明安装它: https : //rstudio.github.io/reticulate/articles/python_packages.html

我不太熟悉 Python,但这适用于我的机器。 见下文:

library(reticulate)

com <- import("win32com.client")

file <- "C:/path/to your/doc.docx"

wordPy <- com$gencache$EnsureDispatch("Word.Application")
wordPyOpen <- wordPy$Documents$Open(file)
wordPyOpen$SaveAs("C:/path/to your/doc.pdf",
                  FileFormat=17)
wordPy$Quit()

希望这会有所帮助!

暂无
暂无

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

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