Skip to content

Instantly share code, notes, and snippets.

@leobarone
Created February 19, 2016 01:00
Show Gist options
  • Save leobarone/8a6127b6942fcb82ac20 to your computer and use it in GitHub Desktop.
Save leobarone/8a6127b6942fcb82ac20 to your computer and use it in GitHub Desktop.
Converter arquivos .pdf em .txt em R
# A funcao readPDF depende da instalacao de pdftotext no seu computador.
# No Windowns - fazer download do xpdf em http://www.foolabs.com/xpdf/download.html
# e deixar os arquivos executaveis no working directory
# No Linux (Ubuntu) - vc fez a escolha certa para a vida e nao precisa fazer nada.
# No MAC - nao sei.
# install.packages("tm")
library(tm)
pastaIn <- "Caminho da pasta onde estao os pdfs"
pastaOut <- "Caminho da pasta para onde irao os txts"
lista.arquivos <- list.files(pastaIn)
for (i in 1:length(lista.arquivos)){
print(i)
nome.arquivo <- lista.arquivos[i]
arquivo <- file.path(pastaIn, nome.arquivo)
texto <- readPDF(control = list(text = "-layout"))(elem = list(uri = arquivo),
language = "pt", id = "id1")
texto <- as.character(texto)
if (length(texto) > 1){
writeLines(texto, file.path(pastaOut, paste0(substr(nome.arquivo, 1, (nchar(nome.arquivo)-3)), "txt")))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment