我试着为一个项目刮掉数百个PDF的文本。
PDF有标题页、标题、页脚和两列。我尝试了pdftools和tabulizer的软件包。然而,两者都有其优点和缺点:
,
pdf_text()函数,正确地读取PDF,只有一些编码问题可以手动解决,但是它没有考虑到两列结构。此外,它生成的字符向量包含的元素与页面相同,相反,tabulizer的文档的文本。
基于堆栈溢出的另一篇文章,我构建了以下基于tabulizer的函数,因为它处理PDF的两列结构,并输出包含存储在单独元素中的所有页面的向量:
get_text <- function(url) {
# Get nunber of pages of PDF
p <- get_n_pages(url)
# Initialize a list
L <- vector(mode = "list", length = 1)
# Extract text from pdf
txt <- tabulizer::extract_text(url, pages = seq(1,p))
# Output: character vector containing all pages
return(txt)
}虽然它在一般情况下工作良好,但也有一些PDF没有正确读取。例如,
get_text(url = "https://aplikace.mvcr.cz/sbirka-zakonu/ViewFile.aspx?type=c&id=3592")显示的不是正确的单词和数字(包含捷克字母),而是“\001\002\r\b\\a\004\t\n\n\n% .\005 \t\031\033 *”。但是,不是所有PDF。此外,请注意pdftools正确读取它(忽略这两列)。
有人能帮我解决这个问题吗?或者解释一下为什么会发生这个问题?
非常感谢您提前!
发布于 2022-09-26 01:14:36
我遇到了这个问题的一些PDF。我使用的一个解决方案是用字符串将数字转换为它们的真实值。以下是一个例子:
convert_Special_Coding_Numbers <- function(text)
{
text <- stringr::str_replace_all(string = text, pattern = "\\003", "")
text <- stringr::str_replace_all(string = text, pattern = "\\025", "2")
text <- stringr::str_replace_all(string = text, pattern = "\\030", "5")
text <- stringr::str_replace_all(string = text, pattern = "\\026", "3")
text <- stringr::str_replace_all(string = text, pattern = "\\034", "9")
text <- stringr::str_replace_all(string = text, pattern = "\\017", ",")
text <- stringr::str_replace_all(string = text, pattern = "\\023", "0")
text <- stringr::str_replace_all(string = text, pattern = "\\027", "4")
return(text)
}https://stackoverflow.com/questions/72229791
复制相似问题