如何将docx转换为PDF?

6

我想问一下是否可以使用R将文本文件(如Word文档或文本文档)转换为PDF?我考虑使用以下代码将其转换为.Rmd,然后再转换为PDF:

require(rmarkdown)
my_text <- readLines("C:/.../track.txt")
cat(my_text, sep="  \n", file = "my_text.Rmd")
render("my_text.Rmd", pdf_document())

但是它并没有起作用,显示了以下错误:
> 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 

有没有其他解决方案?

你在使用哪个操作系统? - Carl Boneri
我正在使用Windows 7。 - Mouna Jmii
你可能需要安装 MikTeXpandoc - Tung
1
“文本文件,如Word文档或文本文档” - 不同类型的文件需要不同的处理程序。您可能希望缩小问题的范围。 - dww
好的,如果您仍在处理项目,这里是答案: https://dev59.com/81YN5IYBdhLWcg3w-cjx#46658645 这篇文章的想法是将那些docx转换成html,然后再转换成pdf,因为我们没有太多选择,这是我已经工作了几天的唯一答案。祝您有美好的一天! - Ralph
您可以查看以下答案:https://dev59.com/l6nka4cB1Zd3GeqPPoz5 - Emmanuel Hamel
3个回答

6

将.txt文件转换为.pdf文件

安装wkhtmltopdf,然后在R中运行以下命令。根据wkhtmltopdf在您系统上的位置以及输入和输出文件路径和名称更改前三行命令。

wkhtmltopdf <- "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
input <- "in.txt"
output <- "out.pdf"
cmd <- sprintf('"%s" "%s" -o "%s"', wkhtmltopdf, input, output)
shell(cmd)

.docx 转 .pdf

安装pandoc,根据需要修改下面的前三行并运行。这个过程的效果可能因您的输入而异。

pandoc <- "C:\\Program Files (x86)\\Pandoc\\pandoc.exe"
input <- "in.docx"
output <- "out.pdf"
cmd <- sprintf('"%s" "%s" -o "%s"', pandoc, input, output)
shell(cmd)

仍然存在错误,我认为这与我的计算机有关: 警告信息: 1:运行命令'C:\ Windows \ system32 \ cmd.exe / c“C:// Program Files(x86)/ Pandoc / pandoc.exe”“C:/ Users / .. / TMP-GAF01-Curriculum Vitae_MJ.doc”-o“C:/ Users / .. / CV_J.pdf”'的状态为1 2:在shell(cmd)中: 执行'“C:// Program Files(x86)/ Pandoc / pandoc.exe”“C:/ Users / ... / TMP-- Curriculum.doc”-o“C:/ Users / ... / CV_J.pdf”'失败,错误代码为1。 - Mouna Jmii
1
问题是关于 .docx 文件的。那与 .doc 不同。 - G. Grothendieck

5

我完全无法使用Pandoc方法。

但是,我找到了一种使用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://learn.microsoft.com/en-us/office/vba/api/word.wdexportformat

希望这可以帮到你!


2
这段代码运行得很好,我只是在退出行之前添加了 wordApp[["ActiveDocument"]]$Close(SaveChanges = 0) 以保存没有更改的文档。 - user3357059
谢谢,注意我的Windows 10机器上此RDCOM方法需要不存在输出。 因此,请在file参数后添加一个对象,以指示输出位置(destination ="C/path_to_my_docx/texte.docx"')。 然后在wordApp[[ActiveDocument"]]$SaveAs(destination, FileFormat = 17)之前添加file.remove(destination)
  • 此方法的文件夹或文件名称中不需要任何空格(即在上面的代码中:file对象和SaveAs('C:/path')中)。
- Clément LVD

3

使用LibreOffice将.docx转换为.pdf

此处所建议的JeanVuda, 您也可以使用LibreOffice将.docx转换为.pdf,前提是您已在计算机上安装了LibreOffice。

以下代码可使用LibreOffice将.docx文件转换为.pdf:

docfile <- "X:/path_to_your_docx/yourdocxfile.docx" 
# Indicate the correct path for the .docx file you want to convert

system(paste("X:/path_to_libreoffice/program/soffice.exe --headless --convert-to pdf", docfile), intern = TRUE)
# Indicate the correct path where libreoffice executable is located on your machine,
# convert .docx to .pdf with libreoffice.

LibreOffice反馈:

  1. 当我的Pandoc版本无法将.docx转换为.pdf且我的R版本不可用于RDCOMClient时,LibreOffice提供了一种快速和直接的方法来将Word文档转换为多种格式。

  2. 请注意,在.pdf转换中,表格在.pdf中无法正确呈现(但以横向模式打印),我发现最直接的方法是使用kableExtra :: as_image()将我的表格转换为图像,并在处理Word文档时进行修补,这可能并不适合您的需求。

  3. 有关命令行转换为其他格式的先前问题,请参见 此处,而我猜ReporteR讨论中介绍此方法供用户使用的原始答案是 那个

最好的问候


这个能否用来批量转换文件夹中的多个文件? - ZR8
是的,您需要使用“for”命令进行配置,以便遍历正确路径列表。例如:1)在您的工作目录中查找一些 .docx 文件并生成它们的路径列表 = docfile = paste0(getwd(), list.files(getwd(), "*.docx"));2)使用“for”进行迭代,以生成多个 PDF - 您需要指定正确的路径以获取 LibreOffice,因为此后它是硬编码的: for(i in 1:length(docfile)){ system(paste("X:/path_to_libreoffice/program/soffice.exe --headless --convert-to pdf", docfile[i]), intern = TRUE)}我还没有测试过这段代码。 - Clément LVD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接