pandoc 或 libreoffice 将多个 .docs 转换为 R 中的 pdf

发布于 2025-01-15 17:10:47 字数 189 浏览 3 评论 0原文

我正在尝试在 R 中将文档从 .docx 批量转换为 .pdf。我的文档包含在以下文件结构中: 1 级文件夹 --> 2 级文件夹 --->文件 在 2 级文件夹中,某些文件已经是 pdf 文件,其他文件是文档文件。

我想知道是否有一种方法可以设置 pandoc 文件路径命令,它读取 2 级文件夹中哪些文件是 .docx,然后进行转换。

I am trying to batch convert documents from .docx to .pdf in R. My documents are contained in the following file structure: Level 1 Folders --> Level 2 Folders ---> Files
Within the level 2 folders, some files are already pdf and others are document.

I'm wondering if there is a way to set up pandoc file path command that it reads which files are .docx within a level 2 folder and then converts those.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2025-01-22 17:10:47

这是一个不使用 pandoc 而是使用 RDCOMClient 的解决方案。我想你可能会感兴趣。

library(RDCOMClient)

path_PDF <- "D:\\temp.pdf"
path_Word <- "D:\\temp.docx"

wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE

############################
#### Open word document ####
############################
doc <- wordApp[["Documents"]]$Open(normalizePath(path_Word),
                                   ConfirmConversions = FALSE)


###################################
#### Save word document as pdf ####
###################################
wordApp[["ActiveDocument"]]$SaveAs(path_PDF, FileFormat = 17) # FileFormat = 17 saves as .PDF
doc$Close()
wordApp$Quit() # quit wordApp

您可以获得要转换的 .docx 文件的列表并使用“for 循环”。

Here is a solution not with pandoc but with RDCOMClient. I thought it might be of interest to you.

library(RDCOMClient)

path_PDF <- "D:\\temp.pdf"
path_Word <- "D:\\temp.docx"

wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE

############################
#### Open word document ####
############################
doc <- wordApp[["Documents"]]$Open(normalizePath(path_Word),
                                   ConfirmConversions = FALSE)


###################################
#### Save word document as pdf ####
###################################
wordApp[["ActiveDocument"]]$SaveAs(path_PDF, FileFormat = 17) # FileFormat = 17 saves as .PDF
doc$Close()
wordApp$Quit() # quit wordApp

You can get a list of the .docx file you want to convert and use a "for loop".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文