import matplotlib.pyplot as plt
import pandas as pd
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
def input_data():
patient_info = {
"姓名": input("请输入患者姓名: "),
"年龄": input("请输入年龄: "),
"性别": input("请输入性别: "),
"检测时间": input("请输入检测时间(YYYY-MM-DD): ")
}
ecg_measurements = {
"心率": float(input("请输入心率(bpm): ")),
"PR间期": float(input("请输入PR间期(ms): ")),
"QRS时限": float(input("请输入QRS时限(ms): ")),
"QT间期": float(input("请输入QT间期(ms): ")),
"QTc": float(input("请输入QTc间期(ms): ")),
"心电轴": float(input("请输入心电轴度数: "))
}
leads = {}
for lead in ["I", "II", "III", "aVR", "aVL", "aVF", "V1", "V2", "V3", "V4", "V5", "V6"]:
leads[lead] = {
"R波振幅": float(input(f"请输入{lead}导联R波振幅(mV): ")),
"ST段偏移": float(input(f"请输入{lead}导联ST段偏移(mV): "))
}
return patient_info, ecg_measurements, leads
def generate_ecg_plot():
time = [i/250 for i in range(1000)]
ecg_signal = [0.5*sin(2*pi*1*i/250) + 0.1*sin(2*pi*10*i/250) for i in range(1000)]
plt.figure(figsize=(10, 6))
plt.plot(time, ecg_signal)
plt.title(" 模拟心电图波形")
plt.xlabel(" 时间 (秒)")
plt.ylabel(" 振幅 (mV)")
plt.grid(True)
plt.savefig("ecg_plot.png")
plt.close()
def generate_pdf(patient_info, ecg_measurements, leads):
doc = SimpleDocTemplate("心电图检测报告.pdf", pagesize=A4)
styles = getSampleStyleSheet()
elements = []
elements.append(Paragraph(" 心电图检测报告", styles['Title']))
elements.append(Spacer(1, 12))
data = [
["患者姓名", patient_info["姓名"]],
["年龄", patient_info["年龄"]],
["性别", patient_info["性别"]],
["检测时间", patient_info["检测时间"]]
]
table = Table(data, colWidths=120)
table.setStyle(TableStyle([('BACKGROUND', (0,0), (1,0), colors.lightblue),
('TEXTCOLOR',(0,0),(1,0),colors.black),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('FONTNAME', (0,0), (-1,-1), 'Helvetica'),
('FONTSIZE', (0,0), (-1,-1), 10),
('BOTTOMPADDING', (0,0), (-1,-1), 6),
('GRID',(0,0),(-1,-1),0.25,colors.black)]))
elements.append(table)
elements.append(Spacer(1, 24))
elements.append(Image("ecg_plot.png", width=500, height=200))
elements.append(Spacer(1, 24))
data = [
["心率", f"{ecg_measurements['心率']} bpm"],
["PR间期", f"{ecg_measurements['PR间期']} ms"],
["QRS时限", f"{ecg_measurements['QRS时限']} ms"],
["QT间期", f"{ecg_measurements['QT间期']} ms"],
["QTc", f"{ecg_measurements['QTc']} ms"],
["心电轴", f"{ecg_measurements['心电轴']}°"]
]
table = Table(data, colWidths=120)
table.setStyle(TableStyle([('BACKGROUND', (0,0), (1,0), colors.lightblue),
('TEXTCOLOR',(0,0),(1,0),colors.black),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('FONTNAME', (0,0), (-1,-1), 'Helvetica'),
('FONTSIZE', (0,0), (-1,-1), 10),
('BOTTOMPADDING', (0,0), (-1,-1), 6),
('GRID',(0,0),(-1,-1),0.25,colors.black)]))
elements.append(table)
elements.append(Spacer(1, 24))
lead_data = []
for lead in leads:
lead_data.append([
lead,
f"{leads[lead]['R波振幅']} mV",
f"{leads[lead]['ST段偏移']} mV"
])
data = [["导联", "R波振幅", "ST段偏移"]] + lead_data
table = Table(data, colWidths=80)
table.setStyle(TableStyle([('BACKGROUND', (0,0), (-1,0), colors.lightblue),
('TEXTCOLOR',(0,0),(-1,0),colors.black),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('FONTNAME', (0,0), (-1,-1), 'Helvetica'),
('FONTSIZE', (0,0), (-1,-1), 10),
('BOTTOMPADDING', (0,0), (-1,-1), 6),
('GRID',(0,0),(-1,-1),0.25,colors.black)]))
elements.append(table)
elements.append(Spacer(1, 24))
elements.append(Paragraph(" 诊断建议:", styles['Heading2']))
elements.append(Paragraph(" 根据测量结果,建议结合临床表现进行综合分析。", styles['Normal']))
doc.build(elements)
if __name__ == "__main__":
patient_info, ecg_measurements, leads = input_data()
generate_ecg_plot()
generate_pdf(patient_info, ecg_measurements, leads)
print("心电图检测报告已生成:心电图检测报告.pdf")