pdf_export.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import json
  2. import os
  3. from c11_api import c11_api
  4. import re
  5. class pdf_export:
  6. api: c11_api
  7. def __init__(self, api):
  8. self.api = api
  9. def export_folder(self, folder, format='PDF'):
  10. reports = api.get_reports_in_folder(folder, True)
  11. for r in reports:
  12. print(r['name'])
  13. self.export_report(r['id'], format, folder=folder)
  14. def export_report(self, report_id, format, params=None, folder=None):
  15. report = api.get_report(report_id)
  16. if format == 'PDF':
  17. path = report['path'].replace(folder, '')
  18. report['filename'] = f"{path}/{report['name']}.pdf"
  19. params = list(re.findall(r'\[([^\]]+)\]', report['filename']))
  20. for i, p in enumerate(params):
  21. report['filename'] = report['filename'].replace('[' + p + ']', '{' + str(i) + '}')
  22. return self.export_pdf(report, params)
  23. if format == 'XML':
  24. return api.export_unstubbed(report_id)
  25. return False
  26. def export_pdf(self, report, params):
  27. if len(params) == 0:
  28. params = {}
  29. filename = report['filename']
  30. api.request_file(report['id'], params, filename)
  31. return True
  32. if len(params) == 1:
  33. params = {}
  34. filename = report['filename'].format('1')
  35. api.request_file(report['id'], params, filename)
  36. key1 = params[0]
  37. for k1, v1 in report['meta']['optional'][key1].items():
  38. filename = report['filename'].format(v1)
  39. params = {key1: {k1: v1}}
  40. api.request_file(report['id'], params, filename)
  41. return True
  42. if len(params) == 2:
  43. key1, key2 = params
  44. for k1, v1 in report['meta']['optional'][key1].items():
  45. for k2, v2 in report['meta']['optional'][key2].items():
  46. filename = report['filename'].format(v1, v2)
  47. params = {key1: {k1: v1}, key2: {k2: v2}}
  48. api.request_file(report['id'], params, filename)
  49. return True
  50. if __name__ == '__main__':
  51. api = c11_api()
  52. api.login()
  53. pdf = pdf_export(api)
  54. # pdf.export_folder('Team Content/Verkauf/1. Gesamtverkauf', 'PDF')
  55. pdf.export_folder('Team Content', 'XML')
  56. reports = api.get_reports_in_folder('Team Content', True)
  57. errors = [r for r in reports if 'error' in r]
  58. filename = 'C:/GlobalCube/Tasks/gctools/logs/config/report_errors.json'
  59. os.makedirs(os.path.dirname(filename), exist_ok=True)
  60. json.dump(errors, open(filename, 'w'), indent=2)