如何将.pdf文件转换为.csv文件

时间:2017-05-01 08:07:31

标签: pdf pdf-scraping

该文件分为各大洲及其国家,我希望各大洲成为列标题。 我尝试过很多东西,但无法执行操作。 这里是link到pdf文件

2 个答案:

答案 0 :(得分:1)

如果它只是这一个文件,那么手动复制和粘贴数据可能最简单。

否则,您需要一个工具将PDF转换为文本(有一些可用的在线和离线工具 - 这最好取决于您的操作系统和情况)。然后你可能需要编写一些脚本来重新排列文本并创建你想要的csv。

答案 1 :(得分:1)

以下是一些可以帮助您的想法 我使用绑定到Tabula java库的包tabulizer" ,它可用于从PDF文档中提取表格" (link)。

library("tabulizer")
continents <- c("Africa","Americas","Asia","Australia/Oceania","Europe")    

f <- "Countries_where_English_is_an_official_language.pdf"
(rawTxt1 <- extract_text(f, page=1, encoding="UTF-8")) 

# Split according to continents   
(rawTxt2 <- strsplit(rawTxt1,"\r\n \r\n")[[1]])

# For trimming leading and trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)

# Organize data in a list 
extrTxt <- vector(length(continents),mode="list")
cnt <- 1
for (k in 1:length(rawTxt2)) {
  rowk <- rawTxt2[k]
  spltxt <- trim(strsplit(rowk,"\r\n")[[1]])
  if (spltxt[1] %in% continents) {
    extrTxt[[cnt]] <- spltxt
    cnt <- cnt +1 
  }
}   
print(extrTxt)

结果如下:

[[1]]
 [1] "Africa"       "Botswana"     "Cameroon"     "Ethiopia"     "Eritrea"      "The Gambia"   "Ghana"        "Kenya"       
 [9] "Lesotho"      "Liberia"      "Malawi"       "Mauritius"    "Namibia"      "Nigeria"      "Rwanda"       "Seychelles"  
[17] "Sierra Leone" "South Africa" "South Sudan"  "Sudan"        "Swaziland"    "Tanzania"     "Uganda"       "Zambia"      
[25] "Zimbabwe"    

[[2]]
 [1] "Americas"                         "Antigua and Barbuda"              "The Bahamas"                     
 [4] "Barbados"                         "Belize"                           "Canada"                          
 [7] "Dominica"                         "Grenada"                          "Guyana"                          
[10] "Jamaica"                          "Saint Kitts and Nevis"            "Saint Lucia"                     
[13] "Saint Vincent and the Grenadines" "Trinidad and Tobago"              "United States"                   

[[3]]
[1] "Asia"        "India"       "Pakistan"    "Philippines" "Singapore"  

[[4]]
 [1] "Australia/Oceania"              "Australia"                      "Fiji"                          
 [4] "Kiribati"                       "Marshall Islands"               "Federated States of Micronesia"
 [7] "Nauru"                          "New Zealand"                    "Palau"                         
[10] "Papua New Guinea"               "Samoa"                          "Solomon Islands"               
[13] "Tonga"                          "Tuvalu"                         "Vanuatu"                       

[[5]]
[1] "Europe"         "Ireland"        "Malta"          "United Kingdom"