order_bounding_boxes_in_each_block.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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):
  47. filename = "app/temporary/" +str(uuid)+"out.html"
  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. for element in result:
  55. new_arr = ""
  56. for x in element:
  57. new_arr += x[4] + " "
  58. if re.search(reg,new_arr):
  59. found = re.findall(reg, new_arr)
  60. for f in found:
  61. if len(f[0]) != 0:
  62. details_.append(f[0].replace(")",""))
  63. if len(f[1]) != 0:
  64. details_.append(f[1])
  65. return details_
  66. def get_tables(result):
  67. reg = r"(Start drawing)|(All dimensions)"
  68. tables = []
  69. for element in result:
  70. new = []
  71. #print(element)
  72. if re.search(reg, element):
  73. new.extend(result[element])
  74. new.append(element)
  75. tables.append(new)
  76. number = len(tables)
  77. #print(tables)
  78. return tables
  79. #file="/home/bscheibel/PycharmProjects/dxf_reader/drawings/5152166_Rev04.html"
  80. #get_bound_box(file)
  81. #pdf_to_html("/home/bscheibel/PycharmProjects/dxf_reader/drawings/5129275_Rev01-GV12.pdf")