function_exctract.py 3.0 KB

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