Converting docx.files to pdf.files with docx2pdf

1k views Asked by At

Not sure what I am doing wrong. I want to convert multiple docx.files to pdf.files - each file into a separate one. I decided to use the "doconv"-package with following command:


docx_files <- list.files(pattern=paste0("Protokollnr_"))[39:73]
docx_files %>% length

lapply(1:35, function(x) {
docx2pdf(input = docx_files[[x]],
         output =  tempfile(fileext = ".pdf"))})

I does not say anything specific in the error message - only that it cannot be converted. Is it that I should have specified the file path - now I only define the file name in my WD.

The object "docx_files" contain:

 c("Protokollnr_1.docx", "Protokollnr_10.docx", "Protokollnr_11.docx", 
    "Protokollnr_12.docx", "Protokollnr_13.docx", "Protokollnr_14.docx", 
    "Protokollnr_15.docx", "Protokollnr_16.docx", "Protokollnr_17.docx", 
    "Protokollnr_18.docx", "Protokollnr_19.docx", "Protokollnr_2.docx", 
    "Protokollnr_20.docx", "Protokollnr_21.docx", "Protokollnr_22.docx", 
    "Protokollnr_23.docx", "Protokollnr_24.docx", "Protokollnr_25.docx", 
    "Protokollnr_26.docx", "Protokollnr_27.docx", "Protokollnr_28.docx", 
    "Protokollnr_29.docx", "Protokollnr_3.docx", "Protokollnr_30.docx", 
    "Protokollnr_31.docx", "Protokollnr_32.docx", "Protokollnr_33.docx", 
    "Protokollnr_34.docx", "Protokollnr_35.docx", "Protokollnr_4.docx", 
    "Protokollnr_5.docx", "Protokollnr_6.docx", "Protokollnr_7.docx", 
    "Protokollnr_8.docx", "Protokollnr_9.docx")

The error message is:

Error in docx2pdf(input = docx_files[[x]], output = tempfile(fileext = ".pdf")) : 
  could not convert C:/Users/Nadine/OneDrive/Documents/Arbeit_Büro_papa/Protokolle_Sallapulka/fertige_Protokolle/Protokollnr_1.docx

Many thanks, Nadine

2

There are 2 answers

8
codable On

I'd recommend specifying the file path since the function requires the following format:

docx2pdf(input, output = gsub("\\.docx$", ".pdf", input))
0
Martin Smith On

I'm encountering this error on Windows because running scripts isn't enabled.

Exploring the source code led me to uncover a more informative error message stating that the temporary script created by doconv "cannot be loaded because running scripts is disabled on this system."

Enabling scripts will require editing execution policies; details are at Microsoft's page about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.