# HG changeset patch # User Sebastien Jodogne # Date 1607161688 -3600 # Node ID d0332c5a2cb80f5fabfefbd2c09ae40975bec9aa # Parent 2c7d6586717b41aedd26b58d03d9c395aefa4d05 Python plugin: excel generation example diff -r 2c7d6586717b -r d0332c5a2cb8 Sphinx/source/plugins/python.rst --- a/Sphinx/source/plugins/python.rst Sat Dec 05 10:22:43 2020 +0100 +++ b/Sphinx/source/plugins/python.rst Sat Dec 05 10:48:08 2020 +0100 @@ -663,6 +663,52 @@ ``orthanc.ChangeType.DELETED``). +.. _python_excel: + +Creating a Microsoft Excel report +................................. + +.. highlight:: python + +As Orthanc plugins have access to any installed Python module, it is +very easy to implement a server-side plugin that generates a report in +the Microsoft Excel ``.xls`` format. Here is a working example:: + + import StringIO + import json + import orthanc + import xlwt + + def CreateExcelReport(output, uri, **request): + if request['method'] != 'GET' : + output.SendMethodNotAllowed('GET') + else: + # Create an Excel writer + excel = xlwt.Workbook() + sheet = excel.add_sheet('Studies') + + # Loop over the studies stored in Orthanc + row = 0 + studies = orthanc.RestApiGet('/studies?expand') + for study in json.loads(studies): + sheet.write(row, 0, study['PatientMainDicomTags'].get('PatientID')) + sheet.write(row, 1, study['PatientMainDicomTags'].get('PatientName')) + sheet.write(row, 2, study['MainDicomTags'].get('StudyDescription')) + row += 1 + + # Serialize the Excel workbook to a string, and return it to the caller + # https://stackoverflow.com/a/15649139/881731 + b = StringIO.StringIO() + excel.save(b) + output.AnswerBuffer(b.getvalue(), 'application/vnd.ms-excel') + + orthanc.RegisterRestCallback('/report.xls', CreateExcelReport) + +If opening the ``http://localhost:8042/report.xls`` URI, this Python +will generate a workbook with one sheet that contains the list of +studies, with the patient ID, the patient name and the study +description. + Performance and concurrency ---------------------------