Category: Python

Using Python to Parse HL7 and CCD Documents in Healthcare

By Stephen Fitzmeyer, MD

Python is a powerful programming language that can be used to parse and manipulate healthcare data in the HL7 and CCD formats. In this article, we will explore how to use Python to extract and process data from HL7 and CCD documents.

First, let’s start by understanding the structure of HL7 and CCD documents. HL7 messages are comprised of segments, which contain fields and subfields that represent different types of data. CCD documents, on the other hand, are based on the HL7 Clinical Document Architecture (CDA) standard and use XML to represent the data.

To parse HL7 messages in Python, we can use the hl7apy library, which is an open-source Python library for working with HL7 messages. Here’s an example of how to use hl7apy to extract patient demographic information from an HL7 message:

from hl7apy.parser import parse_message

# Parse the HL7 message

msg = parse_message(‘MSH|^~\&|HIS|BLG|LIS|BLG|20200528163415||ADT^A04|MSG0001|P|2.3||||||UNICODE’)

# Get the patient name

patient_name =[5][0].value

# Get the patient date of birth

dob =[7].value

# Get the patient sex

sex =[8].value

# Print the patient information

print(“Patient Name: ” + patient_name)

print(“Date of Birth: ” + dob)

print(“Sex: ” + sex)


In this example, we’re using the parse_message() method from the hl7apy library to parse the HL7 message. We then use the message object to extract the patient name, date of birth, and sex from the PID segment.

To parse CCD documents in Python, we can use the ElementTree library, which is included in the Python standard library. Here’s an example of how to use ElementTree to extract medication information from a CCD document:

import xml.etree.ElementTree as ET

# Parse the CCD document

tree = ET.parse(‘ccd.xml’)

# Get the medication section

medications = tree.findall(‘.//{urn:hl7-org:v3}section[@code=”10160-0″]/{urn:hl7-org:v3}entry/{urn:hl7-org:v3}substanceAdministration’)

# Print the medication information

for med in medications:

    drug_name = med.find(‘{urn:hl7-org:v3}consumable/{urn:hl7-org:v3}manufacturedProduct/{urn:hl7-org:v3}manufacturedMaterial/{urn:hl7-org:v3}name/{urn:hl7-org:v3}part’).text

    dosage = med.find(‘{urn:hl7-org:v3}doseQuantity/{urn:hl7-org:v3}value’).text

    start_date = med.find(‘{urn:hl7-org:v3}effectiveTime/{urn:hl7-org:v3}low’).attrib[‘value’]

    end_date = med.find(‘{urn:hl7-org:v3}effectiveTime/{urn:hl7-org:v3}high’).attrib[‘value’]

    print(“Drug Name: ” + drug_name)

    print(“Dosage: ” + dosage)

    print(“Start Date: ” + start_date)

    print(“End Date: ” + end_date)


In this example, we’re using the findall() method from the ElementTree library to find all the medication sections in the CCD document. We then use the find() method to extract the drug name, dosage, start and end date for each medication and print out the results.

Using Python to parse HL7 and CCD documents can be very useful in healthcare applications. For example, we can use these techniques to extract and analyze data from electronic health records (EHRs) to identify patterns and trends in patient care and outcomes. This can help healthcare providers to improve the quality of care, reduce costs, and enhance patient safety.

In conclusion, Python is a powerful tool for parsing and manipulating healthcare data in the HL7 and CCD formats. By using Python to extract and process data from these documents, we can gain valuable insights into patient care and outcomes, which can help to improve healthcare delivery and patient outcomes.

Author: Stephen Fitzmeyer, M.D.
Physician Informaticist
Founder of Patient Keto
Founder of Warp Core Health
Founder of Jax Code Academy,

Connect with Dr. Stephen Fitzmeyer:
Twitter: @PatientKeto

Scroll to top