2

Motivated from the discussion on How to convert docx to PDF in r?, I tried to convert .docx to pdf using the following code.

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)

However, I am getting the "execution failed with error code 1" error. What's the solution? If there is some issue running this in R, how can I do this using other tools?

mb21
  • 34,845
  • 8
  • 116
  • 142
Geet
  • 2,515
  • 2
  • 19
  • 42

1 Answers1

1

I kept having the same problem with this method -- I just can't get it to work.

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

I found the FileFormat=17 bit here https://learn.microsoft.com/en-us/office/vba/api/word.wdexportformat

Edit: Alternative option - Use Python in R via Reticulate package. This uses the pywin32 Python package. If you don't have it, you can install it using instructions found here: https://rstudio.github.io/reticulate/articles/python_packages.html

I'm not as conversant in Python, but this works on my machine. See below:

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()

Hopefully this helps!

  • Thank you! I tried this, but getting an error at wordApp[["Documents"]]$Open(Filename=file) : 80020009 No support for InterfaceSupportsErrorInfo checkErrorInfo -2147352567 Error: Exception occurred. – Geet Dec 16 '18 at 01:11
  • I was having that issue too and realized that I couldn't even open my docx by clicking it. The docx itself was corrupted. That's probably not your issue, but make sure your file is in good order, and double-check the filename you provided for the file object is correct - that's the best advice I can give at this point. – Zachary Smithingell Dec 16 '18 at 01:14
  • I just tried opening that file and it did open. One thing though, it has images and lot of formatting stuff. – Geet Dec 16 '18 at 01:18
  • Even with lots of formatting and images, it seems strange that the open step would trip it up. I'm going to keep obsessing about this and I'll give an update if I find anything. Sorry! – Zachary Smithingell Dec 16 '18 at 01:34
  • By the way, what versions of RDCOMClient and R are you running? – Zachary Smithingell Dec 16 '18 at 01:42
  • It's RDCOMClient_0.93-0 and R version 3.5.1. – Geet Dec 16 '18 at 02:17
  • Alright, I know I have to avoid extended comment discussions, but nothing I can do about that at this point - still a newbie here. I have the same RDCOMClient and R versions. What happens if you skip the `$Add()` step? – Zachary Smithingell Dec 16 '18 at 02:40
  • Still no luck! Same error. I believe there is some small wrinkle that is still there. – Geet Dec 16 '18 at 03:20
  • So strange! It works for me. I've tried multiple variations, changed the file locations and forward and backslash configurations, all kinds of things. The only thing that makes it fail is if I have something messed up in the filename, or if the file is corrupted or otherwise messed up. – Zachary Smithingell Dec 16 '18 at 03:28
  • @Geet: Alright, here's one last possibility: Use Python in R via the Reticulate package. There are times I've had better luck with Python's win32com... If you have (or can get) Python 3.6 or higher and the Reticulate package in R, I can show you the code you need to make it work - I just tried it and it worked. Let me know! – Zachary Smithingell Dec 16 '18 at 04:34
  • Great. Please share that code with me. That would be very helpful. – Geet Dec 16 '18 at 05:07