使用R将PDF文档转换为DOCX?

idfiyjo8  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(149)

我知道这在Python中是可能的,但我想看看在R中是否可能。
我一直在使用下面的网站https://www.viewpdf.com/pdf-to-word.html转换一些pdf输出到word.它似乎工作得很好,并保持所有的格式.但我想知道是否有一种方法可以做到这一点与R?
我看过各种软件包,如pdftools,但找不到太多信息。
谢谢大家。

g6ll5ycj

g6ll5ycj1#

这是我能够想出的解决方案,希望这对未来的任何人都有帮助。

下面是如何在 python中实现的

from pdf2docx import parse

# path of pdf file
pdf_file = 'tests/demo_custom.pdf'

# will create .docx in same path
docx_file = 'tests/demo_custom.docx'

# Here is where we convert pdf to docx
parse(pdf_file, docx_file, start=0, end=None)

以下是如何在 R中执行此操作

记住这一点,我们可以使用reticulate来使用py_run_string()来重用我们的Python代码。

library(reticulate)

py_run_string("from pdf2docx import parse")

# path of pdf file
py_run_string("pdf_file = 'tests/demo_custom.pdf'")

# will create .docx in same path
py_run_string("docx_file = 'tests/demo_custom.docx'")

# Here is where we convert pdf to docx
py_run_string("parse(pdf_file, docx_file, start=0, end=None)")
dohp0rv5

dohp0rv52#

下面是一种可以考虑使用R将PDF文件转换为DOCX的方法:

library(RDCOMClient)

wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE
path_To_PDF_File <- "xxx.pdf"
path_To_Word_File <- "xxx.docx"

doc <- wordApp[["Documents"]]$Open(normalizePath(path_To_PDF_File),
                                   ConfirmConversions = FALSE)
doc$SaveAs2(path_To_Word_File)

相关问题