Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/excel.py @ 927:dfe96daba4f8
python in win installer
author | Alain Mazy <am@osimis.io> |
---|---|
date | Wed, 29 Mar 2023 11:54:58 +0200 |
parents | ba2403ebd4b7 |
children | 600da1bb6acd |
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) |