order_bounding_boxes_in_each_block.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ### FIRST READ EACH BLOCK IN AN ARRAY
  2. from bs4 import BeautifulSoup
  3. import subprocess
  4. import re
  5. def get_bound_box(file):
  6. response = open(file)
  7. html_doc = response.read()
  8. response.close()
  9. html_file = BeautifulSoup(html_doc, 'html.parser')
  10. all_elements = []
  11. blocks = html_file.findAll('block')
  12. number_blocks = len(blocks)
  13. number_words = 0
  14. for block in blocks:
  15. list_elements = []
  16. words = block.findAll('word')
  17. number_words += len(words)
  18. for word in words:
  19. word_list = []
  20. word_list.append(word["xmin"])
  21. word_list.append(word["ymin"])
  22. word_list.append(word["xmax"])
  23. word_list.append(word["ymax"])
  24. word_list.append(word.string)
  25. list_elements.append(word_list)
  26. all_elements.append(list_elements)
  27. #### NEXT SORT ELEMENTS IN EACH BLOCK BY THEIR X AND Y COORDINATES
  28. #### FIRST TRYING XMIN und YMAX
  29. ###FIRST CHECKING IF THE ELEMENTS ARE VERTICAL, IF YES THEN NO SORTING
  30. new_all_elements = []
  31. for element in all_elements:
  32. later_bigger = (float(element[-1][0])-(float(element[0][0]))) #check if xmin from first element is bigger than xmin from last element
  33. abstand_x = abs(float(element[-1][0])-(float(element[0][2])))
  34. abstand_y = abs(float(element[-1][3])-float(element[0][1]))
  35. if later_bigger >= -5:
  36. #print(abstand_x-abstand_y)
  37. new_all_elements.append(element)
  38. else:
  39. new_element = sorted(element, key=lambda k: [float(k[0])])
  40. new_all_elements.append(new_element)
  41. """for element in new_all_elements:
  42. for blub in element:
  43. #print(blub[4])
  44. #print("\n")"""
  45. return new_all_elements, number_blocks, number_words
  46. def pdf_to_html(uuid,filepath, path):
  47. filename = path +"/temporary/" +str(uuid)+"out.html" #to app/temporary later
  48. subprocess.call(['pdftotext', '-bbox-layout',
  49. filepath, filename])
  50. return filename
  51. def extract_isos(result):
  52. reg = r"(ISO\s\d\d\d\d*\W?\d?\W?\d?)|(EN\s\d*)"
  53. details_ = []
  54. reg_general = r"ISO\s?\d*\s*\W\s*[fmcv][HKL]"
  55. general_tol = ""
  56. for element in result:
  57. new_arr = ""
  58. for x in element:
  59. new_arr += x[4] + " "
  60. if re.search(reg,new_arr):
  61. found = re.findall(reg, new_arr)
  62. for f in found:
  63. if len(f[0]) != 0:
  64. details_.append(f[0].replace(")",""))
  65. if len(f[1]) != 0:
  66. details_.append(f[1])
  67. if re.search(reg_general, new_arr):
  68. general_tol = new_arr
  69. return details_, str(general_tol)
  70. def get_tables(result):
  71. reg = r"(Start drawing)|(All dimensions)"
  72. tables = []
  73. for element in result:
  74. new = []
  75. #print(element)
  76. if re.search(reg, element):
  77. new.extend(result[element])
  78. new.append(element)
  79. tables.append(new)
  80. number = len(tables)
  81. #print(tables)
  82. return tables
  83. #file="/home/bscheibel/PycharmProjects/dxf_reader/drawings/5152166_Rev04.html"
  84. #get_bound_box(file)
  85. #pdf_to_html("/home/bscheibel/PycharmProjects/dxf_reader/drawings/5129275_Rev01-GV12.pdf")