12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import json
- import os
- from c11_api import c11_api
- import re
- class pdf_export:
- api: c11_api
- def __init__(self, api):
- self.api = api
- def export_folder(self, folder, format='PDF'):
- reports = api.get_reports_in_folder(folder, True)
- for r in reports:
- print(r['name'])
- self.export_report(r['id'], format, folder=folder)
- def export_report(self, report_id, format, params=None, folder=None):
- report = api.get_report(report_id)
- if format == 'PDF':
- path = report['path'].replace(folder, '')
- report['filename'] = f"{path}/{report['name']}.pdf"
- params = list(re.findall(r'\[([^\]]+)\]', report['filename']))
- for i, p in enumerate(params):
- report['filename'] = report['filename'].replace('[' + p + ']', '{' + str(i) + '}')
- return self.export_pdf(report, params)
- if format == 'XML':
- return api.export_unstubbed(report_id)
- return False
- def export_pdf(self, report, params):
- if len(params) == 0:
- params = {}
- filename = report['filename']
- api.request_file(report['id'], params, filename)
- return True
- if len(params) == 1:
- params = {}
- filename = report['filename'].format('1')
- api.request_file(report['id'], params, filename)
- key1 = params[0]
- for k1, v1 in report['meta']['optional'][key1].items():
- filename = report['filename'].format(v1)
- params = {key1: {k1: v1}}
- api.request_file(report['id'], params, filename)
- return True
- if len(params) == 2:
- key1, key2 = params
- for k1, v1 in report['meta']['optional'][key1].items():
- for k2, v2 in report['meta']['optional'][key2].items():
- filename = report['filename'].format(v1, v2)
- params = {key1: {k1: v1}, key2: {k2: v2}}
- api.request_file(report['id'], params, filename)
- return True
- if __name__ == '__main__':
- api = c11_api()
- api.login()
- pdf = pdf_export(api)
- # pdf.export_folder('Team Content/Verkauf/1. Gesamtverkauf', 'PDF')
- pdf.export_folder('Team Content', 'XML')
- reports = api.get_reports_in_folder('Team Content', True)
- errors = [r for r in reports if 'error' in r]
- filename = 'C:/GlobalCube/Tasks/gctools/logs/config/report_errors.json'
- os.makedirs(os.path.dirname(filename), exist_ok=True)
- json.dump(errors, open(filename, 'w'), indent=2)
|