Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/excel.py @ 1113:a588960a72e5 default tip
spelling
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Mon, 28 Oct 2024 09:23:08 +0100 |
parents | 600da1bb6acd |
children |
rev | line source |
---|---|
976
600da1bb6acd
fix Excel Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
1 import io |
704
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 |
976
600da1bb6acd
fix Excel Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
23 # Serialize the Excel workbook to bytes, and return it to the caller |
600da1bb6acd
fix Excel Python sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
24 b = io.BytesIO() |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 excel.save(b) |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 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
|
27 |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 orthanc.RegisterRestCallback('/report.xls', CreateExcelReport) |