使用官员创建除了docx之外的pdf

问题描述 投票:0回答:2

我在循环中使用官员(用于使用记者)来创建150个独特的文档。我需要这些文件从R导出为word docx和pdfs。

有没有办法将使用官员创建的文档导出为pdf?

r reporters officer
2个回答
1
投票

这是可能的,但我的解决方案取决于libreoffice。这是我正在使用的代码。希望它会有所帮助。我已经硬编码了libreoffice路径,那么你可能需要调整或改进变量cmd_的代码。

该代码正在将PPTX或DOCX文件转换为PDF。

library(pdftools)
office_shot <- function( file, wd = getwd() ){
  cmd_ <- sprintf(
    "/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir %s %s",
    wd, file )
  system(cmd_)

  pdf_file <- gsub("\\.(docx|pptx)$", ".pdf", basename(file))
  pdf_file
}
office_shot(file = "your_presentation.pptx")

0
投票

我一直在使用RDCOMClient将OfficeR创建的docx转换为PDF。

library(RDCOMClient)

file <- "C:/path/to your/doc.docx"
wordApp <- COMCreate("Word.Application") #creates COM object
wordApp[["Documents"]]$Open(Filename=file) #opens your docx in wordApp
wordApp[["ActiveDocument"]]$SaveAs("C:/path/to your/doc.pdf"), FileFormat=17) #saves as PDF 
wordApp$Quit() #quits the COM Word application

我在这里发现了FileFormat = 17位https://docs.microsoft.com/en-us/office/vba/api/word.wdexportformat

我已经能够将上述内容放在一个循环中,以便快速将多个docx转换为PDF。

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.