comparison Sphinx/source/plugins/python.rst @ 551:d0332c5a2cb8

Python plugin: excel generation example
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 05 Dec 2020 10:48:08 +0100
parents fd5c1410db5b
children 4f3a6145ae34
comparison
equal deleted inserted replaced
550:2c7d6586717b 551:d0332c5a2cb8
661 <python-changes>` in the list of studies 661 <python-changes>` in the list of studies
662 (cf. ``orthanc.ChangeType.NEW_STUDY`` and 662 (cf. ``orthanc.ChangeType.NEW_STUDY`` and
663 ``orthanc.ChangeType.DELETED``). 663 ``orthanc.ChangeType.DELETED``).
664 664
665 665
666 .. _python_excel:
667
668 Creating a Microsoft Excel report
669 .................................
670
671 .. highlight:: python
672
673 As Orthanc plugins have access to any installed Python module, it is
674 very easy to implement a server-side plugin that generates a report in
675 the Microsoft Excel ``.xls`` format. Here is a working example::
676
677 import StringIO
678 import json
679 import orthanc
680 import xlwt
681
682 def CreateExcelReport(output, uri, **request):
683 if request['method'] != 'GET' :
684 output.SendMethodNotAllowed('GET')
685 else:
686 # Create an Excel writer
687 excel = xlwt.Workbook()
688 sheet = excel.add_sheet('Studies')
689
690 # Loop over the studies stored in Orthanc
691 row = 0
692 studies = orthanc.RestApiGet('/studies?expand')
693 for study in json.loads(studies):
694 sheet.write(row, 0, study['PatientMainDicomTags'].get('PatientID'))
695 sheet.write(row, 1, study['PatientMainDicomTags'].get('PatientName'))
696 sheet.write(row, 2, study['MainDicomTags'].get('StudyDescription'))
697 row += 1
698
699 # Serialize the Excel workbook to a string, and return it to the caller
700 # https://stackoverflow.com/a/15649139/881731
701 b = StringIO.StringIO()
702 excel.save(b)
703 output.AnswerBuffer(b.getvalue(), 'application/vnd.ms-excel')
704
705 orthanc.RegisterRestCallback('/report.xls', CreateExcelReport)
706
707 If opening the ``http://localhost:8042/report.xls`` URI, this Python
708 will generate a workbook with one sheet that contains the list of
709 studies, with the patient ID, the patient name and the study
710 description.
711
666 712
667 Performance and concurrency 713 Performance and concurrency
668 --------------------------- 714 ---------------------------
669 715
670 **Important:** This section only applies to UNIX-like systems. The 716 **Important:** This section only applies to UNIX-like systems. The