Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from reportlab.lib.pagesizes import letter
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
- from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
- from reportlab.lib.units import inch
- from reportlab.lib import colors
- from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
- from datetime import datetime, timedelta
- from reportlab.pdfgen import canvas
- from reportlab.lib.utils import ImageReader
- from reportlab.lib.validators import isValidQR
- import io
- import os
- def create_medical_document():
- # Create the PDF document
- doc = SimpleDocTemplate("hospital_discharge_summary.pdf", pagesize=letter,
- rightMargin=72, leftMargin=72,
- topMargin=72, bottomMargin=72)
- # Container for the 'Flowable' objects
- elements = []
- # Styles
- styles = getSampleStyleSheet()
- # Custom styles
- title_style = ParagraphStyle(
- 'CustomTitle',
- parent=styles['Heading1'],
- fontSize=18,
- spaceAfter=30,
- alignment=TA_CENTER,
- textColor=colors.darkblue
- )
- heading_style = ParagraphStyle(
- 'CustomHeading',
- parent=styles['Heading2'],
- fontSize=14,
- spaceAfter=12,
- textColor=colors.darkblue
- )
- normal_style = ParagraphStyle(
- 'CustomNormal',
- parent=styles['Normal'],
- fontSize=11,
- spaceAfter=6,
- leading=14
- )
- small_style = ParagraphStyle(
- 'CustomSmall',
- parent=styles['Normal'],
- fontSize=9,
- spaceAfter=3,
- leading=11
- )
- # Calculate dates
- admission_date = datetime(2026, 6, 29)
- discharge_date = admission_date + timedelta(days=14)
- followup_date = discharge_date + timedelta(days=7)
- document_date = discharge_date + timedelta(days=1)
- # Add hospital header with logo placeholder
- header_data = [
- ['<b>SPOTSYLVANIA REGIONAL MEDICAL CENTER</b>', ''],
- ['<b>A Member of the Virginia Health System</b>', ''],
- ['123 Medical Center Drive, Fredericksburg, VA 22408', ''],
- ['Phone: (540) 741-1000 | Fax: (540) 741-1001', ''],
- ['<b>PATIENT DISCHARGE SUMMARY</b>', '']
- ]
- header_table = Table(header_data, colWidths=[5.5*inch, 1.5*inch])
- header_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (0, 0), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (0, 0), 16),
- ('FONTNAME', (0, 1), (0, 1), 'Helvetica'),
- ('FONTSIZE', (0, 1), (0, 1), 10),
- ('FONTNAME', (0, 2), (0, 2), 'Helvetica'),
- ('FONTSIZE', (0, 2), (0, 2), 10),
- ('FONTNAME', (0, 3), (0, 3), 'Helvetica'),
- ('FONTSIZE', (0, 3), (0, 3), 9),
- ('FONTNAME', (0, 4), (0, 4), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 4), (0, 4), 14),
- ('TEXTCOLOR', (0, 4), (0, 4), colors.darkblue),
- ('BOTTOMPADDING', (0, 4), (0, 4), 20),
- ]))
- elements.append(header_table)
- elements.append(Spacer(1, 0.25*inch))
- # Add document info with timestamps
- doc_info_data = [
- ['Document ID:', 'DS-2026-0714-854721369'],
- ['Document Date:', document_date.strftime('%B %d, %Y')],
- ['Document Time:', '10:45 AM'],
- ['Generated By:', 'MEDITECH EHR v6.15'],
- ['Status:', 'FINAL']
- ]
- doc_info_table = Table(doc_info_data, colWidths=[2*inch, 4*inch])
- doc_info_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (-1, -1), 9),
- ('TEXTCOLOR', (0, 0), (-1, -1), colors.grey),
- ('BOTTOMPADDING', (0, 0), (-1, -1), 3),
- ('TOPPADDING', (0, 0), (-1, -1), 3)
- ]))
- elements.append(doc_info_table)
- elements.append(Spacer(1, 0.25*inch))
- # Patient information section
- elements.append(Paragraph("PATIENT INFORMATION", heading_style))
- patient_data = [
- ['<b>Patient Name:</b>', 'Barry Allen Carrington'],
- ['<b>Date of Birth:</b>', 'March 15, 1975 (Age: 51)'],
- ['<b>Medical Record #:</b>', 'MRN-854721369'],
- ['<b>Account #:</b>', 'ACCT-20260629-7854'],
- ['<b>Admission Date:</b>', admission_date.strftime('%B %d, %Y at 4:32 PM')],
- ['<b>Discharge Date:</b>', discharge_date.strftime('%B %d, %Y at 11:15 AM')],
- ['<b>Length of Stay:</b>', '14 days'],
- ['<b>Admitting Physician:</b>', 'Natasha Y. Powell, DO'],
- ['<b>Primary Nurse:</b>', 'NJ'],
- ['<b>Room/Bed:</b>', '312-B'],
- ['<b>Service:</b>', 'Medicine'],
- ['<b>Discharge Disposition:</b>', 'Home']
- ]
- patient_table = Table(patient_data, colWidths=[2.5*inch, 3.5*inch])
- patient_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (-1, -1), 10),
- ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
- ('TOPPADDING', (0, 0), (-1, -1), 5),
- ('LINEBELOW', (0, 0), (-1, -1), colors.grey, 0.5)
- ]))
- elements.append(patient_table)
- elements.append(Spacer(1, 0.25*inch))
- # Attending physician signature section
- elements.append(Paragraph("PHYSICIAN SIGNATURE", heading_style))
- signature_data = [
- ['<b>Attending Physician:</b>', 'Natasha Y. Powell, DO'],
- ['<b>Specialty:</b>', 'Infectious Disease'],
- ['<b>NPI #:</b>', '1659457841'],
- ['<b>Signature:</b>', ''],
- ['', 'Electronically signed on ' + discharge_date.strftime('%B %d, %Y at 11:45 AM')],
- ['', 'Authentication: ★★★★★★★★★★'],
- ['', 'Document ID: SIG-20260713-1145-NP-854721369']
- ]
- signature_table = Table(signature_data, colWidths=[2.5*inch, 3.5*inch])
- signature_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (-1, -1), 10),
- ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
- ('TOPPADDING', (0, 0), (-1, -1), 5),
- ('LINEBELOW', (0, 0), (-1, 0), colors.grey, 0.5),
- ('LINEBELOW', (0, 1), (-1, 1), colors.grey, 0.5),
- ('LINEBELOW', (0, 2), (-1, 2), colors.grey, 0.5),
- ('LINEABOVE', (0, 3), (-1, 3), colors.black, 1),
- ('LINEBELOW', (0, 3), (-1, 3), colors.black, 1),
- ('BOTTOMPADDING', (0, 3), (0, 3), 20)
- ]))
- elements.append(signature_table)
- elements.append(Spacer(1, 0.25*inch))
- # Diagnosis section with ICD-10 codes
- elements.append(Paragraph("DISCHARGE DIAGNOSES (ICD-10-CM)", heading_style))
- diagnosis_data = [
- ['1. B37.9', 'Candidiasis, unspecified - Severe fungal infection with cutaneous manifestations'],
- ['2. N17.9', 'Acute kidney failure, unspecified - Secondary to systemic infection'],
- ['3. R60.9', 'Edema, unspecified - Bilateral lower extremity edema'],
- ['4. L03.915', 'Cellulitis of lower limb - Bilateral lower extremities']
- ]
- diagnosis_table = Table(diagnosis_data, colWidths=[1*inch, 5*inch])
- diagnosis_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (-1, -1), 10),
- ('LEFTPADDING', (0, 0), (0, -1), 20),
- ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
- ('TOPPADDING', (0, 0), (-1, -1), 5)
- ]))
- elements.append(diagnosis_table)
- elements.append(Spacer(1, 0.25*inch))
- # Hospital course summary
- elements.append(Paragraph("HOSPITAL COURSE SUMMARY", heading_style))
- course_text = """
- <b>History of Present Illness:</b> Patient is a 51-year-old male with no significant past medical history
- who presented to the Emergency Department on June 29, 2026 with a 3-day history of progressive swelling
- and erythema of hands and feet. Patient reported inability to perform basic ADLs including holding a cup
- due to severe swelling and pain. No recent travel or sick contacts. No known exposure to fungal agents.
- <b>Hospital Course:</b> Patient was admitted to Medicine service under Dr. Natasha Powell. Initial evaluation
- showed extensive cutaneous involvement with early signs of systemic response. Laboratory studies revealed
- elevated inflammatory markers (WBC 14.3, CRP 12.4 mg/dL) and evidence of acute renal insufficiency
- (Creatinine 2.1 mg/dL, BUN 32 mg/dL). Blood cultures were obtained and initially negative. Dermatology
- consultation was obtained and skin biopsy performed, confirming severe fungal infection. Patient was
- started on intravenous fluconazole 400mg daily and later switched to oral therapy based on sensitivity
- testing. Renal function gradually improved with supportive care and hydration. Physical therapy was
- involved for functional recovery. Patient's condition gradually improved over 14-day hospitalization
- with resolution of acute symptoms and improvement in renal function (discharge Creatinine 1.3 mg/dL).
- <b>Discharge Condition:</b> Patient is afebrile, hemodynamically stable, with significant improvement
- in swelling and pain of extremities. Renal function has returned to near baseline. Patient is able to
- ambulate with minimal assistance and perform basic ADLs.
- """
- elements.append(Paragraph(course_text, normal_style))
- elements.append(Spacer(1, 0.25*inch))
- # Discharge medications with NDC codes
- elements.append(Paragraph("DISCHARGE MEDICATIONS", heading_style))
- medication_data = [
- ['<b>Medication</b>', '<b>Dosage</b>', '<b>Duration</b>', '<b>Refills</b>', '<b>NDC Code</b>'],
- ['Cephalexin', '500 mg orally three times daily', '7 days', '0', 'NDC: 55319-078-01'],
- ['Sulfamethoxazole-Trimethoprim (Bactrim DS)', '1 tablet orally twice daily', '7 days', '0', 'NDC: 51079-513-20'],
- ['Fluconazole', '200 mg orally once daily', '14 days', '0', 'NDC: 60505-3539-1'],
- ['Lisinopril', '10 mg orally once daily', '30 days', '2', 'NDC: 68180-519-10']
- ]
- medication_table = Table(medication_data, colWidths=[2.5*inch, 1.5*inch, 1*inch, 0.8*inch, 1.2*inch])
- medication_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (-1, -1), 9),
- ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
- ('TOPPADDING', (0, 0), (-1, -1), 5),
- ('LINEBELOW', (0, 0), (-1, 0), colors.black, 1),
- ('LINEBELOW', (0, 1), (-1, -1), colors.grey, 0.5)
- ]))
- elements.append(medication_table)
- elements.append(Spacer(1, 0.25*inch))
- # Follow-up instructions
- elements.append(Paragraph("FOLLOW-UP INSTRUCTIONS", heading_style))
- followup_data = [
- ['<b>Specialist</b>', '<b>Clinic</b>', '<b>Date</b>', '<b>Phone</b>', '<b>Status</b>'],
- ['Infectious Disease', 'KHAEN', followup_date.strftime('%m/%d/%Y'), '(540) 741-2000', 'Scheduled'],
- ['Primary Care', 'Fredericksburg Family Practice', (followup_date + timedelta(days=7)).strftime('%m/%d/%Y'), '(540) 373-4000', 'Pending'],
- ['Nephrology', 'Virginia Kidney Specialists', (followup_date + timedelta(days=14)).strftime('%m/%d/%Y'), '(540) 741-3100', 'Pending']
- ]
- followup_table = Table(followup_data, colWidths=[1.5*inch, 2*inch, 1*inch, 1.5*inch, 1*inch])
- followup_table.setStyle(TableStyle([
- ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
- ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
- ('FONTSIZE', (0, 0), (-1, -1), 9),
- ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
- ('TOPPADDING', (0, 0), (-1, -1), 5),
- ('LINEBELOW', (0, 0), (-1, 0), colors.black, 1),
- ('LINEBELOW', (0, 1), (-1, -1), colors.grey, 0.5)
- ]))
- elements.append(followup_table)
- elements.append(Spacer(1, 0.25*inch))
- # Patient instructions
- elements.append(Paragraph("PATIENT INSTRUCTIONS", heading_style))
- instructions_text = """
- <b>Medications:</b> Take all prescribed medications as directed. Do not skip doses. Complete full course of antibiotics.
- <b>Wound Care:</b> Keep hands and feet clean and dry. Avoid prolonged moisture exposure. Change socks daily.
- <b>Activity:</b> Limit activity until follow-up appointment. Avoid prolonged standing or walking until swelling resolves.
- <b>Diet:</b> Follow renal diet as provided. Limit sodium intake to less than 2g per day. Drink 8-10 glasses of water daily unless otherwise directed.
- <b>Monitoring:</b> Monitor for signs of infection recurrence (fever, increased redness, swelling, pain). Check temperature daily.
- <b>Return to ED:</b> Return to emergency department immediately if experiencing fever >101°F, worsening swelling, difficulty breathing, or decreased urine output.
- """
- elements.append(Paragraph(instructions_text, normal_style))
- elements.append(Spacer(1, 0.5*inch))
- # Add page break before footer
- elements.append(PageBreak())
- # Footer with confidentiality notice
- footer_text = """
- <b>CONFIDENTIALITY NOTICE:</b> This document contains confidential patient information protected by federal and state law.
- Unauthorized disclosure is prohibited. If you have received this document in error, please notify us immediately and destroy the document.
- <b>HIPAA PRIVACY NOTICE:</b> This information is being disclosed to you with the patient's written authorization.
- You may not re-disclose this information to any other person or entity without additional written authorization from the patient.
- <b>DOCUMENT AUTHENTICATION:</b> This document was generated through the MEDITECH EHR system on """ + document_date.strftime('%B %d, %Y at %I:%M %P') + """
- and is electronically signed by the attending physician. This document is a legal medical record and is admissible in court.
- <b>QUESTIONS:</b> If you have any questions regarding this document, please contact Health Information Management at (540) 741-1200.
- """
- elements.append(Paragraph(footer_text, small_style))
- elements.append(Spacer(1, 0.25*inch))
- # Add hospital contact information
- contact_info = """
- <b>SPOTSYLVANIA REGIONAL MEDICAL CENTER</b><br/>
- 123 Medical Center Drive<br/>
- Fredericksburg, VA 22408<br/>
- Phone: (540) 741-1000<br/>
- Fax: (540) 741-1001<br/>
- Website: www.spotsylvaniahealth.org<br/>
- """
- elements.append(Paragraph(contact_info, small_style))
- # Custom page function to add watermark and page numbers
- def add_page_number(canvas, doc):
- # Add watermark
- canvas.saveState()
- canvas.setFont("Helvetica", 50)
- canvas.setFillColor(colors.grey, alpha=0.1)
- canvas.drawCentredText(letter[0]/2, letter[1]/2, "CONFIDENTIAL")
- canvas.restoreState()
- # Add page number
- canvas.saveState()
- canvas.setFont("Helvetica", 9)
- canvas.setFillColor(colors.grey)
- canvas.drawRightString(letter[0]-0.75*inch, 0.5*inch, "Page %d" % (doc.page))
- canvas.restoreState()
- # Build the document with custom page function
- doc.build(elements, onFirstPage=add_page_number, onLaterPages=add_page_number)
- print("Enhanced medical document created successfully: hospital_discharge_summary.pdf")
- # Run the function
- if __name__ == "__main__":
- create_medical_document()
Advertisement
Add Comment
Please, Sign In to add comment