pdf_merge.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import config
  2. from PyPDF2 import PdfMerger
  3. from pathlib import Path
  4. import re
  5. import json
  6. from datetime import datetime
  7. def pdf_merge(base_dir: str, report_name, config=None):
  8. if config is None:
  9. config = {"_0": []}
  10. report_prefix = re.sub(r"\[([^\]]+)\]", "", report_name)
  11. if report_prefix[-2:] == "__":
  12. report_prefix = report_prefix[:-1]
  13. for suffix, entries in config.items():
  14. target = f"{base_dir}/{report_prefix}{suffix}.pdf"
  15. if Path(target).exists():
  16. Path(target).unlink()
  17. pdfs = [f for f in Path(base_dir).glob(report_prefix + "*.pdf")]
  18. pdfs.sort(key=lambda f: f.stat().st_mtime)
  19. if len(entries) > 0:
  20. pdfs = [f for e in entries for f in pdfs if e + ".pdf" in f.name]
  21. if len(pdfs) > 0:
  22. with PdfMerger() as merger:
  23. for pdf in pdfs:
  24. merger.append(pdf)
  25. merger.write(target)
  26. def pdf_merge_files(file_dict: dict[str, list[str]]):
  27. max_age = datetime.now().timestamp() - 12 * 60 * 60
  28. for target, file_list in file_dict.items():
  29. if target[-4:] != ".pdf":
  30. continue
  31. pdfs = [
  32. f
  33. for f in file_list
  34. if Path(f).exists() and Path(f).stat().st_mtime > max_age
  35. ]
  36. if len(pdfs) == 0:
  37. continue
  38. try:
  39. with PdfMerger() as merger:
  40. for pdf in pdfs:
  41. merger.append(pdf)
  42. merger.write(target)
  43. except PermissionError:
  44. print(target)
  45. print("--> not accessible!")
  46. def get_reports(reports_file, folder):
  47. with open(reports_file, "r") as frh:
  48. reports = json.load(frh)
  49. return [r["name"] for r in reports if r["path"].startswith(folder)]
  50. def merge_reports_in_folder(
  51. cfg: config.Config, folder="Team Content/ReportOutput", config=None
  52. ):
  53. reports = get_reports(cfg.cognos11.reports_file, folder)
  54. print(folder)
  55. folder2 = folder[26:]
  56. for r in reports:
  57. print(folder2 + "/" + r)
  58. pdf_merge(cfg.cognos11.reportoutput_dir + "/" + folder2, r, config)
  59. def get_config_json(config, config_dir):
  60. if config == "":
  61. return None
  62. if config.startswith("{"):
  63. return json.loads(config)
  64. config_file = f"{config_dir}/merge/{config}.json"
  65. if not Path(config_file).exists():
  66. return None
  67. with open(config_file, "r") as frh:
  68. return json.load(frh)
  69. if __name__ == "__main__":
  70. merge_reports_in_folder(config.Config())