下载库

pip install reportlab

字体文件

微软雅黑
链接:https://pan.baidu.com/s/1N2uxR97yx42xiJDDXR_S0A 
提取码:ocbw 
--来自百度网盘超级会员V5的分享
其他字体请自行解决
完整版
if is_print:
    """
    打印 pdf 
    """
    u = r.data
    file_name = u['name'] + '病例信息.pdf'

    buffer = io.BytesIO()
    p = canvas.Canvas(buffer)
    pdfmetrics.registerFont(TTFont('MicrosoftYaHei', 'MicrosoftYaHei.ttf'))  # 注册自定义字体
    #  敲黑板 , 划重点 字体文件存放位子
    #  MicrosoftYaHei.ttf 存放于\site-packages\reportlab\fonts\MicrosoftYaHei.ttf
    p.setFont("MicrosoftYaHei", 18)  # 设置初始字体 及字体大小

    styles = getSampleStyleSheet()
    styles.add(ParagraphStyle(fontName='MicrosoftYaHei', name='MicrosoftYaHei'))  # 自己增加新注册的字体

    p.drawString(90, 780, "卒中绿色通道时间节点管理表", charSpace=16)
    """
    mode  字体粗体 / 文本 等 类型
    charSpace  字符间隔大小
    """
    log.info('标题结束'.rjust(50, '-'))
    log.info('基本信息开始'.rjust(50, '-'))

    elements = []
    data = [
        ['姓名', u['name'], '年龄', u['age'], ],
        ['性别', u['sex'], '医保卡号', u['medicare_id'], ],
        ['就诊ID', u['see_doctor_id'], '住院ID', u['hospitalized_id'], ],
    ]
    t = Table(data, colWidths=[135, 135, 135, 135], rowHeights=30)
    """
    colWidths = 表格列宽  [int,int]  int   列表每列单独设置  , 数字统一设置
    rowHeights = 表格行高
    """

    t.setStyle(
        TableStyle(
            [
                ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),  # 字体颜色
                ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),  # 网格颜色
                ('BOX', (0, 0), (-1, -1), 0.25, colors.black),  # 盒子颜色
                ("FONTNAME", (0, 0), (-1, -1), 'MicrosoftYaHei'),  # 指定中文字体
                # ('ALIGN', (0, 0), (-1, -1), 'CENTER'),  # 设定行左右居中对齐
                ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 设定行上下居中对齐
                ('FONTSIZE', (0, 0), (-1, -1), 12),  # 设定字体大小

            ]
        )
    )
    elements.append(t)

    t.wrapOn(p, 600, 200)  # 宽度  高度  文档是这样写的 , 但是设置了好像没什么用 , 不设置又不实行 [裂开]
    t.drawOn(p, 30, 670)  # x轴  y轴

    log.info('基本信息结束'.rjust(50, '-'))

    log.info('患者到院开始'.rjust(50, '-'))

    elements = []
    data = [
        ['患者到达医院时间', u.get('goto_hospital_time', ''), '急诊护士接诊患者时间',
         u.get('emergency_triage', {}).get('emergency_triage_time', ''), ],
        ['采血时间', u.get('blood_collection'), '送检时间', u.get('blood_send_test_time'), ],
        ['心电图检测完成时间', u.get('ecg_check', {}).get('ecg_time', ''), '-'],
    ]
    t = Table(data, colWidths=135, rowHeights=30)
    t.setStyle(TableStyle([
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),  # 字体颜色
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),  # 网格颜色
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),  # 盒子颜色
        ("FONTNAME", (0, 0), (-1, -1), 'MicrosoftYaHei'),  # 指定中文字体
        ('ALIGN', (2, 2), (-1, -1), 'CENTER'),  # 设定行左右居中对齐
        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 设定行上下居中对齐
        ('FONTSIZE', (0, 0), (-1, -1), 12),  # 设定字体大小
        ('SPAN', (2, 2), (-1, -1)),  # 合并单元格
    ]))
    elements.append(t)
    t.wrapOn(p, 600, 200)  # 宽度  高度
    t.drawOn(p, 30, 560)  # x轴  y轴

    log.info('患者到院结束'.rjust(50, '-'))

    log.info('患者发病开始'.rjust(50, '-'))

    elements = []
    data = [
        ['发病时间', u.get('outbreak_time', {}).get('morbidity_time', ''), '末次正常时间(发病\n时间不明确)',
         u.get('outbreak_time', {}).get('last_normal_time', ''), ],
        ['患者到达CT室时间', u.get('arrival_CT_time', ''), 'CT检查结束时间',
         u.get('image_examination', {}).get('image_examination_list', [{}])[0].get('content', {}).get(
             'examination_done_time', ''), ],
        ['CT报告时间',
         u.get('image_examination', {}).get('image_examination_list', [{}])[0].get('content', {}).get(
             'examination_report_time', ''), 'MRI检查结束时间',
         u.get('image_examination', {}).get('image_examination_list', [{}, {}, {}, {}])[3].get(
             'content', {}).get(
             'examination_done_time', ''), ],
        ['MRI报告时间',
         u.get('image_examination', {}).get('image_examination_list', [{}, {}, {}, {}])[3].get(
             'content', {}).get(
             'examination_report_time', ''), '-'],
    ]
    t = Table(data, colWidths=135, rowHeights=30)
    t.setStyle(TableStyle([
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),  # 字体颜色
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),  # 网格颜色
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),  # 盒子颜色
        ("FONTNAME", (0, 0), (-1, -1), 'MicrosoftYaHei'),  # 指定中文字体
        ('ALIGN', (2, 3), (-1, -1), 'CENTER'),  # 设定行左右居中对齐
        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 设定行上下居中对齐
        ('FONTSIZE', (0, 0), (-1, -1), 12),  # 设定字体大小
        ('SPAN', (2, 3), (-1, -1)),  # 合并单元格
    ]))
    elements.append(t)
    t.wrapOn(p, 600, 200)  # 宽度  高度
    t.drawOn(p, 30, 420)  # x轴  y轴

    log.info('患者发病结束'.rjust(50, '-'))

    log.info('患者血液检查开始'.rjust(50, '-'))

    elements = []
    data = [
        ['血常规报告时间', u.get('laboratory_examination', {}).get('blood_routine_time', ''), '凝血常规报告时间',
         u.get('laboratory_examination', {}).get('cohesion_blood_routine_time', ''), ],
        ['肾功能及电解质\n报告时间',
         u.get('laboratory_examination', {}).get('renal_function_electrolytes_time', ''), '-'],
    ]
    t = Table(data, colWidths=135, rowHeights=30)
    t.setStyle(TableStyle([
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),  # 字体颜色
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),  # 网格颜色
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),  # 盒子颜色
        ("FONTNAME", (0, 0), (-1, -1), 'MicrosoftYaHei'),  # 指定中文字体
        ('ALIGN', (2, 1), (-1, -1), 'CENTER'),  # 设定行左右居中对齐
        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 设定行上下居中对齐
        ('FONTSIZE', (0, 0), (-1, -1), 12),  # 设定字体大小
        ('SPAN', (2, 1), (-1, -1)),  # 合并单元格
    ]))
    elements.append(t)
    t.wrapOn(p, 600, 200)  # 宽度  高度
    t.drawOn(p, 30, 340)  # x轴  y轴

    log.info('患者血液检查结束'.rjust(50, '-'))

    log.info('患者溶栓治疗开始'.rjust(50, '-'))

    timeline_dict = {i['timeTypeStr']: i['timeOutTime'] for i in u.get('timeline', []) if
                     i.get('timeTypeStr')}

    elements = []
    data = [
        ['溶栓医生接诊患者时间',
         u.get('thrombolytic_doctor_accept', {}).get('thrombolytic_doctor_accept_time', ''),
         '静脉溶栓开始谈话时间', u.get('thrombolytic_consent', {}).get('first_knowing_time', ''), ],
        ['静脉溶栓患者\n家属签字时间', u.get('thrombolytic_consent', {}).get('sign_knowing', ''), '启动溶栓时间',
         u.get('start_thrombolytic_time', ''), ],
        ['DNT', timeline_dict.get('DNT', ''), '-'],
    ]
    t = Table(data, colWidths=135, rowHeights=30)
    t.setStyle(TableStyle([
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),  # 字体颜色
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),  # 网格颜色
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),  # 盒子颜色
        ("FONTNAME", (0, 0), (-1, -1), 'MicrosoftYaHei'),  # 指定中文字体
        ('ALIGN', (2, 2), (-1, -1), 'CENTER'),  # 设定行左右居中对齐
        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 设定行上下居中对齐
        ('FONTSIZE', (0, 0), (-1, -1), 12),  # 设定字体大小
        ('SPAN', (2, 2), (-1, -1)),  # 合并单元格
    ]))
    elements.append(t)
    t.wrapOn(p, 600, 200)  # 宽度  高度
    t.drawOn(p, 30, 230)  # x轴  y轴

    log.info('患者溶栓治疗结束'.rjust(50, '-'))

    log.info('患者介入治疗开始'.rjust(50, '-'))
    elements = []
    data = [
        ['介入手术预警时间', u.get('surgery_warning', {}).get('surgery_warning_time', ''), '介入医生接诊时间',
         u.get('interventional_doctor_receives', {}).get('interventional_doctor_receives_time', ''), ],
        ['介入治疗谈话时间', u.get('interventional_consent', {}).get('first_knowing_time', ''), '介入治疗家属签字时间',
         u.get('interventional_consent', {}).get('sign_knowing', ''), ],
        ['启动导管室时间', u.get('start_catheter', {}).get('start_catheter_time', ''), '完成穿刺时间',
         u.get('puncture_completion', {}).get('done_time', '')],
        ['DPT', timeline_dict.get('DPT', ''), '-'],
    ]
    t = Table(data, colWidths=135, rowHeights=30)
    t.setStyle(TableStyle([
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.black),  # 字体颜色
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),  # 网格颜色
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),  # 盒子颜色
        ("FONTNAME", (0, 0), (-1, -1), 'MicrosoftYaHei'),  # 指定中文字体
        ('ALIGN', (2, 3), (-1, -1), 'CENTER'),  # 设定行左右居中对齐
        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 设定行上下居中对齐
        ('FONTSIZE', (0, 0), (-1, -1), 12),  # 设定字体大小
        ('SPAN', (2, 3), (-1, -1)),  # 合并单元格
    ]))
    elements.append(t)
    t.wrapOn(p, 600, 200)  # 宽度  高度
    t.drawOn(p, 30, 90)  # x轴  y轴

    log.info('患者介入治疗结束'.rjust(50, '-'))
    log.info('导出PDF结束'.rjust(50, '-'))

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()

    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    buffer.seek(0)
    return FileResponse(buffer, as_attachment=True, filename=file_name)
End
文章作者:
文章地址:http://honglvliyu.cn/index.php/archives/54/
文章标题:Python-打印PDF
文章版权:本文由Beyond the game独立整理撰写,转载请保留文章出处。
Last modification:June 23rd, 2022 at 07:20 pm
咋滴 看了不给钱呀
广而告知