v1ral_ITS

hostpital_doc.py

Jul 13th, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.24 KB | None | 0 0
  1. from reportlab.lib.pagesizes import letter
  2. from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
  3. from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
  4. from reportlab.lib.units import inch
  5. from reportlab.lib import colors
  6. from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
  7. from datetime import datetime, timedelta
  8. from reportlab.pdfgen import canvas
  9. from reportlab.lib.utils import ImageReader
  10. from reportlab.lib.validators import isValidQR
  11. import io
  12. import os
  13.  
  14. def create_medical_document():
  15.     # Create the PDF document
  16.     doc = SimpleDocTemplate("hospital_discharge_summary.pdf", pagesize=letter,
  17.                            rightMargin=72, leftMargin=72,
  18.                            topMargin=72, bottomMargin=72)
  19.    
  20.     # Container for the 'Flowable' objects
  21.     elements = []
  22.    
  23.     # Styles
  24.     styles = getSampleStyleSheet()
  25.    
  26.     # Custom styles
  27.     title_style = ParagraphStyle(
  28.         'CustomTitle',
  29.         parent=styles['Heading1'],
  30.         fontSize=18,
  31.         spaceAfter=30,
  32.         alignment=TA_CENTER,
  33.         textColor=colors.darkblue
  34.     )
  35.    
  36.     heading_style = ParagraphStyle(
  37.         'CustomHeading',
  38.         parent=styles['Heading2'],
  39.         fontSize=14,
  40.         spaceAfter=12,
  41.         textColor=colors.darkblue
  42.     )
  43.    
  44.     normal_style = ParagraphStyle(
  45.         'CustomNormal',
  46.         parent=styles['Normal'],
  47.         fontSize=11,
  48.         spaceAfter=6,
  49.         leading=14
  50.     )
  51.    
  52.     small_style = ParagraphStyle(
  53.         'CustomSmall',
  54.         parent=styles['Normal'],
  55.         fontSize=9,
  56.         spaceAfter=3,
  57.         leading=11
  58.     )
  59.    
  60.     # Calculate dates
  61.     admission_date = datetime(2026, 6, 29)
  62.     discharge_date = admission_date + timedelta(days=14)
  63.     followup_date = discharge_date + timedelta(days=7)
  64.     document_date = discharge_date + timedelta(days=1)
  65.    
  66.     # Add hospital header with logo placeholder
  67.     header_data = [
  68.         ['<b>SPOTSYLVANIA REGIONAL MEDICAL CENTER</b>', ''],
  69.         ['<b>A Member of the Virginia Health System</b>', ''],
  70.         ['123 Medical Center Drive, Fredericksburg, VA 22408', ''],
  71.         ['Phone: (540) 741-1000 | Fax: (540) 741-1001', ''],
  72.         ['<b>PATIENT DISCHARGE SUMMARY</b>', '']
  73.     ]
  74.    
  75.     header_table = Table(header_data, colWidths=[5.5*inch, 1.5*inch])
  76.     header_table.setStyle(TableStyle([
  77.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  78.         ('FONTNAME', (0, 0), (0, 0), 'Helvetica-Bold'),
  79.         ('FONTSIZE', (0, 0), (0, 0), 16),
  80.         ('FONTNAME', (0, 1), (0, 1), 'Helvetica'),
  81.         ('FONTSIZE', (0, 1), (0, 1), 10),
  82.         ('FONTNAME', (0, 2), (0, 2), 'Helvetica'),
  83.         ('FONTSIZE', (0, 2), (0, 2), 10),
  84.         ('FONTNAME', (0, 3), (0, 3), 'Helvetica'),
  85.         ('FONTSIZE', (0, 3), (0, 3), 9),
  86.         ('FONTNAME', (0, 4), (0, 4), 'Helvetica-Bold'),
  87.         ('FONTSIZE', (0, 4), (0, 4), 14),
  88.         ('TEXTCOLOR', (0, 4), (0, 4), colors.darkblue),
  89.         ('BOTTOMPADDING', (0, 4), (0, 4), 20),
  90.     ]))
  91.    
  92.     elements.append(header_table)
  93.     elements.append(Spacer(1, 0.25*inch))
  94.    
  95.     # Add document info with timestamps
  96.     doc_info_data = [
  97.         ['Document ID:', 'DS-2026-0714-854721369'],
  98.         ['Document Date:', document_date.strftime('%B %d, %Y')],
  99.         ['Document Time:', '10:45 AM'],
  100.         ['Generated By:', 'MEDITECH EHR v6.15'],
  101.         ['Status:', 'FINAL']
  102.     ]
  103.    
  104.     doc_info_table = Table(doc_info_data, colWidths=[2*inch, 4*inch])
  105.     doc_info_table.setStyle(TableStyle([
  106.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  107.         ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
  108.         ('FONTSIZE', (0, 0), (-1, -1), 9),
  109.         ('TEXTCOLOR', (0, 0), (-1, -1), colors.grey),
  110.         ('BOTTOMPADDING', (0, 0), (-1, -1), 3),
  111.         ('TOPPADDING', (0, 0), (-1, -1), 3)
  112.     ]))
  113.    
  114.     elements.append(doc_info_table)
  115.     elements.append(Spacer(1, 0.25*inch))
  116.    
  117.     # Patient information section
  118.     elements.append(Paragraph("PATIENT INFORMATION", heading_style))
  119.    
  120.     patient_data = [
  121.         ['<b>Patient Name:</b>', 'Barry Allen Carrington'],
  122.         ['<b>Date of Birth:</b>', 'March 15, 1975 (Age: 51)'],
  123.         ['<b>Medical Record #:</b>', 'MRN-854721369'],
  124.         ['<b>Account #:</b>', 'ACCT-20260629-7854'],
  125.         ['<b>Admission Date:</b>', admission_date.strftime('%B %d, %Y at 4:32 PM')],
  126.         ['<b>Discharge Date:</b>', discharge_date.strftime('%B %d, %Y at 11:15 AM')],
  127.         ['<b>Length of Stay:</b>', '14 days'],
  128.         ['<b>Admitting Physician:</b>', 'Natasha Y. Powell, DO'],
  129.         ['<b>Primary Nurse:</b>', 'NJ'],
  130.         ['<b>Room/Bed:</b>', '312-B'],
  131.         ['<b>Service:</b>', 'Medicine'],
  132.         ['<b>Discharge Disposition:</b>', 'Home']
  133.     ]
  134.    
  135.     patient_table = Table(patient_data, colWidths=[2.5*inch, 3.5*inch])
  136.     patient_table.setStyle(TableStyle([
  137.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  138.         ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
  139.         ('FONTSIZE', (0, 0), (-1, -1), 10),
  140.         ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
  141.         ('TOPPADDING', (0, 0), (-1, -1), 5),
  142.         ('LINEBELOW', (0, 0), (-1, -1), colors.grey, 0.5)
  143.     ]))
  144.    
  145.     elements.append(patient_table)
  146.     elements.append(Spacer(1, 0.25*inch))
  147.    
  148.     # Attending physician signature section
  149.     elements.append(Paragraph("PHYSICIAN SIGNATURE", heading_style))
  150.    
  151.     signature_data = [
  152.         ['<b>Attending Physician:</b>', 'Natasha Y. Powell, DO'],
  153.         ['<b>Specialty:</b>', 'Infectious Disease'],
  154.         ['<b>NPI #:</b>', '1659457841'],
  155.         ['<b>Signature:</b>', ''],
  156.         ['', 'Electronically signed on ' + discharge_date.strftime('%B %d, %Y at 11:45 AM')],
  157.         ['', 'Authentication: ★★★★★★★★★★'],
  158.         ['', 'Document ID: SIG-20260713-1145-NP-854721369']
  159.     ]
  160.    
  161.     signature_table = Table(signature_data, colWidths=[2.5*inch, 3.5*inch])
  162.     signature_table.setStyle(TableStyle([
  163.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  164.         ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
  165.         ('FONTSIZE', (0, 0), (-1, -1), 10),
  166.         ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
  167.         ('TOPPADDING', (0, 0), (-1, -1), 5),
  168.         ('LINEBELOW', (0, 0), (-1, 0), colors.grey, 0.5),
  169.         ('LINEBELOW', (0, 1), (-1, 1), colors.grey, 0.5),
  170.         ('LINEBELOW', (0, 2), (-1, 2), colors.grey, 0.5),
  171.         ('LINEABOVE', (0, 3), (-1, 3), colors.black, 1),
  172.         ('LINEBELOW', (0, 3), (-1, 3), colors.black, 1),
  173.         ('BOTTOMPADDING', (0, 3), (0, 3), 20)
  174.     ]))
  175.    
  176.     elements.append(signature_table)
  177.     elements.append(Spacer(1, 0.25*inch))
  178.    
  179.     # Diagnosis section with ICD-10 codes
  180.     elements.append(Paragraph("DISCHARGE DIAGNOSES (ICD-10-CM)", heading_style))
  181.    
  182.     diagnosis_data = [
  183.         ['1. B37.9', 'Candidiasis, unspecified - Severe fungal infection with cutaneous manifestations'],
  184.         ['2. N17.9', 'Acute kidney failure, unspecified - Secondary to systemic infection'],
  185.         ['3. R60.9', 'Edema, unspecified - Bilateral lower extremity edema'],
  186.         ['4. L03.915', 'Cellulitis of lower limb - Bilateral lower extremities']
  187.     ]
  188.    
  189.     diagnosis_table = Table(diagnosis_data, colWidths=[1*inch, 5*inch])
  190.     diagnosis_table.setStyle(TableStyle([
  191.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  192.         ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
  193.         ('FONTSIZE', (0, 0), (-1, -1), 10),
  194.         ('LEFTPADDING', (0, 0), (0, -1), 20),
  195.         ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
  196.         ('TOPPADDING', (0, 0), (-1, -1), 5)
  197.     ]))
  198.    
  199.     elements.append(diagnosis_table)
  200.     elements.append(Spacer(1, 0.25*inch))
  201.    
  202.     # Hospital course summary
  203.     elements.append(Paragraph("HOSPITAL COURSE SUMMARY", heading_style))
  204.    
  205.     course_text = """
  206.    <b>History of Present Illness:</b> Patient is a 51-year-old male with no significant past medical history
  207.    who presented to the Emergency Department on June 29, 2026 with a 3-day history of progressive swelling
  208.    and erythema of hands and feet. Patient reported inability to perform basic ADLs including holding a cup
  209.    due to severe swelling and pain. No recent travel or sick contacts. No known exposure to fungal agents.
  210.    
  211.    <b>Hospital Course:</b> Patient was admitted to Medicine service under Dr. Natasha Powell. Initial evaluation
  212.    showed extensive cutaneous involvement with early signs of systemic response. Laboratory studies revealed
  213.    elevated inflammatory markers (WBC 14.3, CRP 12.4 mg/dL) and evidence of acute renal insufficiency
  214.    (Creatinine 2.1 mg/dL, BUN 32 mg/dL). Blood cultures were obtained and initially negative. Dermatology
  215.    consultation was obtained and skin biopsy performed, confirming severe fungal infection. Patient was
  216.    started on intravenous fluconazole 400mg daily and later switched to oral therapy based on sensitivity
  217.    testing. Renal function gradually improved with supportive care and hydration. Physical therapy was
  218.    involved for functional recovery. Patient's condition gradually improved over 14-day hospitalization
  219.    with resolution of acute symptoms and improvement in renal function (discharge Creatinine 1.3 mg/dL).
  220.    
  221.    <b>Discharge Condition:</b> Patient is afebrile, hemodynamically stable, with significant improvement
  222.    in swelling and pain of extremities. Renal function has returned to near baseline. Patient is able to
  223.    ambulate with minimal assistance and perform basic ADLs.
  224.    """
  225.    
  226.     elements.append(Paragraph(course_text, normal_style))
  227.     elements.append(Spacer(1, 0.25*inch))
  228.    
  229.     # Discharge medications with NDC codes
  230.     elements.append(Paragraph("DISCHARGE MEDICATIONS", heading_style))
  231.    
  232.     medication_data = [
  233.         ['<b>Medication</b>', '<b>Dosage</b>', '<b>Duration</b>', '<b>Refills</b>', '<b>NDC Code</b>'],
  234.         ['Cephalexin', '500 mg orally three times daily', '7 days', '0', 'NDC: 55319-078-01'],
  235.         ['Sulfamethoxazole-Trimethoprim (Bactrim DS)', '1 tablet orally twice daily', '7 days', '0', 'NDC: 51079-513-20'],
  236.         ['Fluconazole', '200 mg orally once daily', '14 days', '0', 'NDC: 60505-3539-1'],
  237.         ['Lisinopril', '10 mg orally once daily', '30 days', '2', 'NDC: 68180-519-10']
  238.     ]
  239.    
  240.     medication_table = Table(medication_data, colWidths=[2.5*inch, 1.5*inch, 1*inch, 0.8*inch, 1.2*inch])
  241.     medication_table.setStyle(TableStyle([
  242.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  243.         ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
  244.         ('FONTSIZE', (0, 0), (-1, -1), 9),
  245.         ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
  246.         ('TOPPADDING', (0, 0), (-1, -1), 5),
  247.         ('LINEBELOW', (0, 0), (-1, 0), colors.black, 1),
  248.         ('LINEBELOW', (0, 1), (-1, -1), colors.grey, 0.5)
  249.     ]))
  250.    
  251.     elements.append(medication_table)
  252.     elements.append(Spacer(1, 0.25*inch))
  253.    
  254.     # Follow-up instructions
  255.     elements.append(Paragraph("FOLLOW-UP INSTRUCTIONS", heading_style))
  256.    
  257.     followup_data = [
  258.         ['<b>Specialist</b>', '<b>Clinic</b>', '<b>Date</b>', '<b>Phone</b>', '<b>Status</b>'],
  259.         ['Infectious Disease', 'KHAEN', followup_date.strftime('%m/%d/%Y'), '(540) 741-2000', 'Scheduled'],
  260.         ['Primary Care', 'Fredericksburg Family Practice', (followup_date + timedelta(days=7)).strftime('%m/%d/%Y'), '(540) 373-4000', 'Pending'],
  261.         ['Nephrology', 'Virginia Kidney Specialists', (followup_date + timedelta(days=14)).strftime('%m/%d/%Y'), '(540) 741-3100', 'Pending']
  262.     ]
  263.    
  264.     followup_table = Table(followup_data, colWidths=[1.5*inch, 2*inch, 1*inch, 1.5*inch, 1*inch])
  265.     followup_table.setStyle(TableStyle([
  266.         ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
  267.         ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
  268.         ('FONTSIZE', (0, 0), (-1, -1), 9),
  269.         ('BOTTOMPADDING', (0, 0), (-1, -1), 5),
  270.         ('TOPPADDING', (0, 0), (-1, -1), 5),
  271.         ('LINEBELOW', (0, 0), (-1, 0), colors.black, 1),
  272.         ('LINEBELOW', (0, 1), (-1, -1), colors.grey, 0.5)
  273.     ]))
  274.    
  275.     elements.append(followup_table)
  276.     elements.append(Spacer(1, 0.25*inch))
  277.    
  278.     # Patient instructions
  279.     elements.append(Paragraph("PATIENT INSTRUCTIONS", heading_style))
  280.    
  281.     instructions_text = """
  282.    <b>Medications:</b> Take all prescribed medications as directed. Do not skip doses. Complete full course of antibiotics.
  283.    
  284.    <b>Wound Care:</b> Keep hands and feet clean and dry. Avoid prolonged moisture exposure. Change socks daily.
  285.    
  286.    <b>Activity:</b> Limit activity until follow-up appointment. Avoid prolonged standing or walking until swelling resolves.
  287.    
  288.    <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.
  289.    
  290.    <b>Monitoring:</b> Monitor for signs of infection recurrence (fever, increased redness, swelling, pain). Check temperature daily.
  291.    
  292.    <b>Return to ED:</b> Return to emergency department immediately if experiencing fever >101°F, worsening swelling, difficulty breathing, or decreased urine output.
  293.    """
  294.    
  295.     elements.append(Paragraph(instructions_text, normal_style))
  296.     elements.append(Spacer(1, 0.5*inch))
  297.    
  298.     # Add page break before footer
  299.     elements.append(PageBreak())
  300.    
  301.     # Footer with confidentiality notice
  302.     footer_text = """
  303.    <b>CONFIDENTIALITY NOTICE:</b> This document contains confidential patient information protected by federal and state law.
  304.    Unauthorized disclosure is prohibited. If you have received this document in error, please notify us immediately and destroy the document.
  305.    
  306.    <b>HIPAA PRIVACY NOTICE:</b> This information is being disclosed to you with the patient's written authorization.
  307.    You may not re-disclose this information to any other person or entity without additional written authorization from the patient.
  308.    
  309.    <b>DOCUMENT AUTHENTICATION:</b> This document was generated through the MEDITECH EHR system on """ + document_date.strftime('%B %d, %Y at %I:%M %P') + """
  310.    and is electronically signed by the attending physician. This document is a legal medical record and is admissible in court.
  311.    
  312.    <b>QUESTIONS:</b> If you have any questions regarding this document, please contact Health Information Management at (540) 741-1200.
  313.    """
  314.    
  315.     elements.append(Paragraph(footer_text, small_style))
  316.     elements.append(Spacer(1, 0.25*inch))
  317.    
  318.     # Add hospital contact information
  319.     contact_info = """
  320.    <b>SPOTSYLVANIA REGIONAL MEDICAL CENTER</b><br/>
  321.    123 Medical Center Drive<br/>
  322.    Fredericksburg, VA 22408<br/>
  323.    Phone: (540) 741-1000<br/>
  324.    Fax: (540) 741-1001<br/>
  325.    Website: www.spotsylvaniahealth.org<br/>
  326.    """
  327.    
  328.     elements.append(Paragraph(contact_info, small_style))
  329.    
  330.     # Custom page function to add watermark and page numbers
  331.     def add_page_number(canvas, doc):
  332.         # Add watermark
  333.         canvas.saveState()
  334.         canvas.setFont("Helvetica", 50)
  335.         canvas.setFillColor(colors.grey, alpha=0.1)
  336.         canvas.drawCentredText(letter[0]/2, letter[1]/2, "CONFIDENTIAL")
  337.         canvas.restoreState()
  338.        
  339.         # Add page number
  340.         canvas.saveState()
  341.         canvas.setFont("Helvetica", 9)
  342.         canvas.setFillColor(colors.grey)
  343.         canvas.drawRightString(letter[0]-0.75*inch, 0.5*inch, "Page %d" % (doc.page))
  344.         canvas.restoreState()
  345.    
  346.     # Build the document with custom page function
  347.     doc.build(elements, onFirstPage=add_page_number, onLaterPages=add_page_number)
  348.     print("Enhanced medical document created successfully: hospital_discharge_summary.pdf")
  349.  
  350. # Run the function
  351. if __name__ == "__main__":
  352.     create_medical_document()
Add Comment
Please, Sign In to add comment