Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. require 'iguvium'
  2. require 'csv'
  3.  
  4. pages = Iguvium.read('WL_P04.pdf')
  5.  
  6. def pdfToCSV tables,flag
  7. array = tables.first.to_a
  8.  
  9. table_column = []
  10. table_rows = []
  11. table = []
  12.  
  13. array[3..-1].each do |row|
  14.  
  15. row.each_with_index do |col, c|
  16. table_column = col.delete(',').split(' ').to_a.map(&:to_i)
  17.  
  18. table_column.each_with_index do |v, l|
  19. if c == 0
  20. table_rows << [v]
  21. else
  22. table_rows[l] << v
  23. end
  24. end
  25. end
  26. table << [table_rows]
  27. table_rows = []
  28. end
  29.  
  30. if flag
  31. CSV.open("WL_P01#Y05.csv", "w") do |csv|
  32. array[0..2].each do |header|
  33. csv << header
  34. end
  35. end
  36. end
  37.  
  38. CSV.open("WL_P01#Y05.csv", "a+") do |csv|
  39. table.each do |data|
  40. data.each do |row|
  41. row.each do |val|
  42. csv << val
  43. end
  44. end
  45. end
  46. end
  47. end
  48.  
  49. pdfToCSV pages[7].extract_tables!,true
  50. # pdfToCSV pages[18].extract_tables!,false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement