如何在R中将docx转换为PDF? [英] How to convert docx to PDF in r?
问题描述
我想问问是否可以使用R将Word文档或文本文档之类的文本文件转换为PDF? 我想使用此代码将其转换为.rmd,然后转换为PDF
I want to ask if it is possible to convert text files such as word document or text document to PDF using R ? I thought of converting it to .rmd and then to PDF using this code
require(rmarkdown)
my_text <- readLines("C:/.../track.txt")
cat(my_text, sep=" \n", file = "my_text.Rmd")
render("my_text.Rmd", pdf_document())
但是显示此错误不起作用:
But it doesn't work showing this error:
错误:无法编译my_text.tex. 另外:警告消息: 正在运行的命令'"pdflatex"-错误停止-interaction = batchmode"my_text.tex"'的状态为127
Error: Failed to compile my_text.tex. In addition: Warning message: running command '"pdflatex" -halt-on-error -interaction=batchmode "my_text.tex"' had status 127
还有其他解决方法吗?
推荐答案
我绝对无法使Pandoc方法对我有用.
I absolutely have not been able to make the Pandoc method work for me.
但是,我确实找到了一种使用RDCOMClient将docx转换为PDF的方法.
I did figure out a way to convert docx to PDF using RDCOMClient, however.
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
I found the FileFormat=17 bit here https://docs.microsoft.com/en-us/office/vba/api/word.wdexportformat
希望这会有所帮助!
这篇关于如何在R中将docx转换为PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!