annotate Sphinx/source/plugins/python/excel.py @ 842:92ca063536ed Orthanc-1.11.0

Orthanc 1.11.0
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 May 2022 10:50:15 +0200
parents ba2403ebd4b7
children 600da1bb6acd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
704
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 import StringIO
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 import json
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 import orthanc
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 import xlwt
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 def CreateExcelReport(output, uri, **request):
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 if request['method'] != 'GET' :
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 output.SendMethodNotAllowed('GET')
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 else:
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # Create an Excel writer
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 excel = xlwt.Workbook()
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 sheet = excel.add_sheet('Studies')
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # Loop over the studies stored in Orthanc
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 row = 0
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 studies = orthanc.RestApiGet('/studies?expand')
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 for study in json.loads(studies):
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 sheet.write(row, 0, study['PatientMainDicomTags'].get('PatientID'))
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 sheet.write(row, 1, study['PatientMainDicomTags'].get('PatientName'))
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 sheet.write(row, 2, study['MainDicomTags'].get('StudyDescription'))
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 row += 1
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 # Serialize the Excel workbook to a string, and return it to the caller
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 # https://stackoverflow.com/a/15649139/881731
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 b = StringIO.StringIO()
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 excel.save(b)
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 output.AnswerBuffer(b.getvalue(), 'application/vnd.ms-excel')
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
ba2403ebd4b7 moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 orthanc.RegisterRestCallback('/report.xls', CreateExcelReport)