function_exctract.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import pandas as pd
  2. import os
  3. import glob
  4. def extract_pdf_path(filename):
  5. name_without_ext = filename.replace(".pdf", "")
  6. parts = name_without_ext.rsplit("_", 1)
  7. entreprise_name = parts[0]
  8. année_extraction = parts[1]
  9. # Chemin basé sur l'emplacement du fichier, pas le dossier courant
  10. script_dir = os.path.dirname(os.path.abspath(__file__))
  11. folder_abs = os.path.join(script_dir, "..", "01 - Sources", année_extraction)
  12. folder_abs = os.path.abspath(folder_abs)
  13. #print(f"CHEMIN : {folder_abs}")
  14. #print(f"EXISTE : {os.path.exists(folder_abs)}")
  15. #print(f"CONTENU : {os.listdir(folder_abs) if os.path.exists(folder_abs) else 'DOSSIER INTROUVABLE'}")
  16. all_pdfs = glob.glob(os.path.join(folder_abs, "*.pdf"))
  17. entreprise_lower = entreprise_name.strip().lower()
  18. matches = [
  19. p for p in all_pdfs
  20. if os.path.basename(p).lower().startswith(entreprise_lower)
  21. ]
  22. if matches:
  23. return matches[0]
  24. else:
  25. raise FileNotFoundError(
  26. f"Aucun PDF trouvé pour '{entreprise_name}' en {année_extraction}\n"
  27. f"PDFs disponibles :\n" +
  28. "\n".join([os.path.basename(p) for p in all_pdfs])
  29. )
  30. def extract_pages(filename):
  31. # Extrait entreprise_name et année depuis le nom du fichier
  32. name_without_ext = filename.replace(".pdf", "")
  33. parts = name_without_ext.rsplit("_", 1)
  34. entreprise_name = parts[0]
  35. année_extraction = int(parts[1])
  36. # Chemin basé sur l'emplacement du fichier
  37. script_dir = os.path.dirname(os.path.abspath(__file__))
  38. excel_path = os.path.abspath(os.path.join(script_dir, "..", "02 - Inputs", "_QRTs_paramétrages_Abd.xlsx"))
  39. # Lecture du fichier Excel
  40. df = pd.read_excel(excel_path, sheet_name="Liste SFCR", header=3)
  41. df.columns = [str(col).strip() for col in df.columns]
  42. # Trouve la colonne Entité (insensible à l'encodage)
  43. entite_col = [col for col in df.columns if "ntit" in col][0]
  44. # Filtre la ligne correspondante
  45. row = df[
  46. (df[entite_col].str.strip().str.lower() == entreprise_name.strip().lower()) &
  47. (df["Année"] == année_extraction)
  48. ]
  49. if row.empty:
  50. raise ValueError(f"Aucune ligne trouvée pour '{entreprise_name}' en {année_extraction}")
  51. row = row.iloc[0]
  52. # Mapping colonnes Excel -> sections SFCR
  53. section_columns = {
  54. "S.02": "Pages S.02",
  55. "S.04": "Pages S.04",
  56. "S.05": "Pages S.05",
  57. "S.12": "Pages S.12",
  58. "S.17": "Pages S.17",
  59. "S.19": "Pages S.19",
  60. "S.22": "Pages S.22",
  61. "S.23": "Pages S.23",
  62. "S.25": "Pages S.25",
  63. "S.28": "Pages S.28",
  64. }
  65. sections = {}
  66. for section, col in section_columns.items():
  67. val = row.get(col, None)
  68. if pd.notna(val) and str(val).strip() != "":
  69. # Convertit "135, 136" -> [135, 136]
  70. pages = [int(p.strip()) for p in str(val).split(",")]
  71. sections[section] = pages
  72. else:
  73. sections[section] = []
  74. return sections