【问题标题】:How to convert docx to PDF?如何将docx转换为PDF?
【发布时间】:2018-08-13 06:54:33
【问题描述】:

我想问是否可以使用 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 

还有其他解决办法吗?

【问题讨论】:

  • 你正在开发什么操作系统?
  • 我使用的是 Windows 7
  • 您可能需要安装MikTeXpandoc
  • "文本文件,例如 word 文档或文本文档" - 不同类型的文件需要不同的程序。您可能希望缩小问题的范围
  • 好的,如果您仍在处理您的项目,这就是答案stackoverflow.com/a/46658645/15027157 该帖子的想法是将这些 docx 转换为 html 而不是 pdf,因为我们没有太多选择,那就是我工作了几天的唯一答案祝你有美好的一天!

标签: r pdf shiny package text-files


【解决方案1】:

.docx 到 .pdf 与 libreoffice

根据JeanVuda 的建议here,您还可以使用 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 并且 RDCOMClient 不适用于我的 R 版本,libreoffice 提供了一种快速直接的方法来转换多种格式的 word 文档。

  2. 请注意,对于 .pdf 转换,表格在 .pdf 中无法正确呈现(但以横向模式打印),我能找到的最直接的方法是在图像中转换我的表格在使用 kableExtra::as_image() 编织 word 文档的过程中,这可能不适合您的需要。

  3. 以前有关于命令行转换为其他格式here的问题,我猜在ReporteR讨论中为用户介绍这种方法的原始答案是that one

最好的问候

【讨论】:

    【解决方案2】:

    我绝对无法让 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://docs.microsoft.com/en-us/office/vba/api/word.wdexportformat

    希望这会有所帮助!

    【讨论】:

    • 这段代码运行良好,我只是在 Quit 行之前添加了wordApp[["ActiveDocument"]]$Close(SaveChanges = 0) 以保存文档而不做任何更改。
    • 谢谢,请注意,在我的 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')
    【解决方案3】:

    .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: running command '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) : l'execution de '"C://Program Files (x86)/Pandoc/pandoc.exe" "C:/Users/.../TMP-- Curriculum.doc" -o "C:/ Users/.../CV_J.pdf"' a échoué avec le code d'erreur 1.
    • 问题是关于.docx 文件。这与.doc相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2014-04-20
    • 2017-05-12
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多