我有一个文件夹中的.pdf文件列表,我想首先访问文本的前两段然后将它们存储在.csv文件中,我能够转换pdf文本但不能先提取两段。
这就是我试过的
setwd("D/All_PDF_Files")
install.packages("pdftools")
install.packages("qdapRegex")
library(pdftools)
library(qdapRegex)
All_files=Sys.glob("*.pdf")
txt <- pdf_text("first.pdf")
cat(txt[1])
rm_between(txt, 'This ', '1. ', extract=TRUE)[[1]]
但是这给了我“ NA ”
cat(txt [1])的输出为:
"Maharashtra Real Estate Regulatory Authority
REGISTRATION CERTIFICATE OF PROJECT
FORM 'C'
[See rule 6(a)]
This registration is granted under section 5 of the Act to the following project under project registration number :
P52100000255
Project: Ganga Legend A3 And B3.., Plot Bearing / CTS / Survey / Final Plot No.: Sr No 305 P , 306 P and 339 P ,
Village Bavdhan Budruk, Taluka Mulashi,District Pune at Pune (M Corp.), Pune City, Pune, 411001;
1. Goel Ganga Developers (I) Pvt Ltd having its registered office / principal place of business at Tehsil: Pune City,
District: Pune, Pin: 411001.
2. This registration is granted subject to the following conditions, namely:"
我想要提取的是文本
This registration is granted under section 5 of the Act to the following project under project registration number :
P52100000255
Project: Ganga Legend A3 And B3.., Plot Bearing / CTS / Survey / Final Plot No.: Sr No 305 P , 306 P and 339 P ,
Village Bavdhan Budruk, Taluka Mulashi,District Pune at Pune (M Corp.), Pune City, Pune, 411001;
是否有任何人有更好的方法,任何建议将不胜感激。
感谢。
答案 0 :(得分:1)
希望这有帮助!
library(pdftools)
setwd("D/All_PDF_Files")
All_files=Sys.glob("*.pdf")
df <- data.frame()
for (i in 1:length(All_files))
{
txt <- pdf_text(All_files[i])
file_name <- All_files[i]
#skip first 4 header rows (you may need to adjust this count according to your use case)
FirstPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[1+4]
SecondPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[2+4]
df <- rbind(df, cbind(file_name, FirstPara, SecondPara))
}
df
答案 1 :(得分:0)
如果有人需要,可以使用@ Prem的代码发布答案。
All_files=Sys.glob("*.pdf")
df <- data.frame()
for (i in 1:length(All_files))
{
txt <- pdf_text(All_files[i])
file_name <- All_files[i]
FirstPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[1+4]
SecondPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[2+4]
ThirdPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[3+4]
ThirdPara_new <- sub("[^:]+:\\s*([^,]+),.*", "\\1",ThirdPara)
t1=unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[4+4]
t2=unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[5+4]
conct=paste(t1,t2)
FourthPara=gsub(".*1. \\s*|having.*|son.*", "", conct)
df <- rbind(df, cbind(file_name, SecondPara, ThirdPara_new, FourthPara))
}