我正在使用 pdftools 包中的 read_pdf() 函式逐行讀取 PDF 檔案,但突然間沒有更改腳本中的任何內容、任何引數或行,它開始讀取整個頁面,而不是逐行分隔元素。我如何讓它回到逐行分隔?這是我可以使用文本挖掘來構建我需要的資料庫的唯一方法。
uj5u.com熱心網友回復:
使用以下代碼,您可以通過直接讀取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)
uj5u.com熱心網友回復:
使用以下代碼,您可以將 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513780.html
標籤:rpdf文本挖掘
