如何在R中逐行阅读PDF



我使用pdftools包中的read_pdf((函数逐行读取pdf文件,但突然间,它开始读取整个页面,而不是逐行分隔元素。如何使其返回到逐行分隔?这是我可以使用文本挖掘来构建所需数据库的唯一方法。

使用以下代码,您可以通过直接读取PDF文件来逐行发送文本

library(pdftools)
library(pagedown)
chrome_print(input = "https://en.wikipedia.org/wiki/Cat", 
output = "D:\Text_PDF_Cat.pdf")
text <- pdf_text("D:\Text_PDF_Cat.pdf")
text <- lapply(X = text, FUN = function(x) strsplit(x, "n"))
text <- unlist(text)

使用以下代码,您可以将PDF文件转换为Word,然后将其保存为txt文件。之后,您可以读取txt文件的第一行:

library(RDCOMClient)
library(pagedown)
#############################################
#### Step 1 : Save wikipedia page as PDF ####
#############################################
chrome_print(input = "https://en.wikipedia.org/wiki/Cat", 
output = "D:\Text_PDF_Cat.pdf")
path_PDF <- "D:\Text_PDF_Cat.pdf"
path_Word <- "D:\Text_PDF_Cat.docx"
path_Txt <- "D:\Text_PDF_Cat.txt"
################################################################
#### Step 2 : Convert PDF to word document with OCR of Word ####
################################################################
wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE
doc <- wordApp[["Documents"]]$Open(normalizePath(path_PDF), ConfirmConversions = FALSE)  # convert pdf file to word / an OCR is included in word
doc$SaveAs(path_Txt, FileFormat = 4) # Save file to txt
#########################################
#### Step 3 : Read first line of txt ####
#########################################
readLines(path_Txt, n = 1)

最新更新