pdf_parser.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. from collections import defaultdict
  3. import pandas as pd
  4. import tika
  5. from tika import parser
  6. from utils import (
  7. extract_filing_date,
  8. address_extraction,
  9. extract_reference,
  10. email_extraction,
  11. telephone_number_extraction,
  12. )
  13. tika.initVM()
  14. def main(PATH):
  15. """
  16. parses the required data from the pdfs
  17. """
  18. data_dict = defaultdict(list)
  19. required_files = [file for file in os.listdir(PATH) if file.find(".pdf") != -1]
  20. for file in required_files:
  21. content = parser.from_file(file)["content"]
  22. data_dict["document_name"].append(file)
  23. data_dict["filing_date"].append(extract_filing_date(content))
  24. data_dict["address"].append(address_extraction(content))
  25. data_dict["references"].append(extract_reference(content))
  26. data_dict["email"].append(email_extraction(content))
  27. data_dict["telephone_number"].append(telephone_number_extraction(content))
  28. data = pd.DataFrame(data_dict)
  29. data.to_csv("required_data.csv")
  30. if __name__ == "__main__":
  31. HOME_DIR = os.path.expanduser("~")
  32. BASE_DIR = "Code/pdf_parser/pdfs"
  33. PATH = os.path.join(HOME_DIR, BASE_DIR)
  34. main(PATH)