changeset 551:d0332c5a2cb8

Python plugin: excel generation example
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 05 Dec 2020 10:48:08 +0100
parents 2c7d6586717b
children 5327509a5031
files Sphinx/source/plugins/python.rst
diffstat 1 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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
 ---------------------------