This project provides a simple Python script to generate HL7 messages. HL7 (Health Level 7) is a set of international standards for the exchange, integration, sharing, and retrieval of electronic health information.
- generate_hl7.py: The main Python script to generate HL7 messages.
- patient_info.json: A sample JSON file containing patient information.
- Python 3.x
- hl7 library (You can install it using
pip install hl7)
-
Install the HL7 library:
pip install hl7
-
Prepare the patient information: Modify the
patient_info.jsonfile with the relevant patient information. -
Run the script: Execute the
generate_hl7.pyscript to generate the HL7 message.python generate_hl7.py
-
Output: The script will generate an HL7 message and save it to
output.hl7.
{
"patient_id": "123456",
"first_name": "John",
"last_name": "Doe",
"dob": "19800101",
"gender": "M",
"address": "123 Main St, Anytown, USA",
"marital_status": "S"
}import hl7
import json
def create_hl7_message(patient_info):
# Create HL7 message
message = hl7.Message()
# MSH Segment
msh = hl7.Segment("MSH")
msh.append(["MSH", "|", "^", "sending_app", "sending_facility", "receiving_app", "receiving_facility", "202401010101", "", "ADT^A01", "12345", "P", "2.3"])
message.append(msh)
# PID Segment
pid = hl7.Segment("PID")
pid.append(["PID", "1", patient_info["patient_id"], "", f"{patient_info['last_name']}^{patient_info['first_name']}", "", patient_info["dob"], patient_info["gender"], "", patient_info["address"], "", "", "", "", "", "", "", patient_info["marital_status"]])
message.append(pid)
return message
def save_hl7_message(message, filename):
with open(filename, "w") as file:
file.write(str(message))
def load_patient_info(filename):
with open(filename, "r") as file:
return json.load(file)
if __name__ == "__main__":
patient_info_file = "patient_info.json"
hl7_output_file = "output.hl7"
# Load patient information from JSON file
patient_info = load_patient_info(patient_info_file)
# Create HL7 message
hl7_message = create_hl7_message(patient_info)
# Save HL7 message to a file
save_hl7_message(hl7_message, hl7_output_file)
print(f"HL7 message saved to {hl7_output_file}")The script will generate the following HL7 message and save it to output.hl7:
MSH|^~\&|sending_app|sending_facility|receiving_app|receiving_facility|202401010101||ADT^A01|12345|P|2.3
PID|1|123456||Doe^John||19800101|M||123 Main St, Anytown, USA||||||||||S