python - 如何通过reportlab将表格定位在特定的x和y坐标
问题描述
我有以下生成pdf的代码:
def colr(x, y, z):
return (x/255, y/255, z/255)
import reportlab
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.utils import ImageReader
from reportlab.platypus import SimpleDocTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph
from reportlab.platypus.tables import Table
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
width, height = A4
logo = '/home/joel/myappointments/appointments/static/clinic/img/logo/logo.png'
elements = []
print(f'Height={height}')
imgw = imgh = 100
im = Image(logo, width=imgw, height=imgh)
im.hAlign = 'LEFT'
elements.append(im)
headstyle = ParagraphStyle(
name='MyHeader',
fontName='Helvetica-Bold',
fontSize=16,
leading =10
)
doctorstyle = ParagraphStyle(
name='MyDoctorHeader',
fontName='Helvetica',
fontSize=13,
leading =10
)
data = [[Paragraph("Dr John Doe's ENT Clinic", style = headstyle)], [Paragraph("Dr John Doe", style = doctorstyle)], [Paragraph("ENT Specialist", style = doctorstyle)], [Paragraph("Registration No. ", style = doctorstyle)]]
elements.append(Table(data, repeatRows=1))
line1 = ("Name", "Test", "Age", "20yr")
line2 = ("MRD No.", "18","Date", "14-11-2018")
line3 = ("No.","#", "Doctor", "Dr.John Doe")
data=[line1,line2, line3]
patientdetailstable = Table(data)
patientdetailstable.setStyle(TableStyle([
('BACKGROUND', (0, 0), (4, 0), '#CFEAD4'),
('BACKGROUND', (0, 2), (4, 2), '#CFEAD4'),
('BOX',(0,0),(-1,-1), 0.5, '#CFEAD4'),
('GRID',(0,0),(-1,-1), 0.5, colr(12, 43, 8)),
]))
elements.append(patientdetailstable)
elements.append(Spacer(1, 20))
# We use paragraph style because we need to wrap text. We cant directly wrap cells otherwise
line1 = ["Sl.", "Medicine" , "Dose", "Freq", "Durn", "Note"]
drug1 = Paragraph('AUGMED Syrup 30ml (AMOXICILLIN 200MG + CLAVULANATE(CLAVULANIC ACID) 28.5MG)', styleN)
line2 = ["1", drug1, "1 Tab", "1-0-1", "5 days", ""]
line3 = ["2", drug1, "1 Tab", "1-0-1", "5 days", ""]
data=[line1,line2, line3]
for i in range(3,50):
temp = [str(i), "Some Drug here", "5 ml", "1-0-1", "10 days", "No comments"]
data.append(temp)
medstable = Table(data, repeatRows=1)
medstable.setStyle(TableStyle([
('VALIGN',(0,0),(-1,-1), 'TOP'),
('TEXTCOLOR',(0,0),(-1,0),colors.white),
('BACKGROUND', (0, 0), (-1, 0), colr(40, 196, 15)),
('GRID',(0,1),(-1,-1), 0.5, '#CFEAD4'),
]))
elements.append(medstable)
doc = SimpleDocTemplate('output.pdf', pagesize=A4, rightMargin=20, leftMargin=20, \
topMargin=20, bottomMargin=20, allowSplitting=1,\
title="Prescription", author="MyOPIP.com")
doc.build(elements)
一切都很好,除了我需要的部分:在John Doe's ENT clinic
徽标Registration No
的右侧,而不是在其下方。如果我直接使用 Canvas 而不是可流动的 SimpleDocTemplate,我可以轻松设置它的位置。
使用 SimpleDocTemplate,如何任意定位该部分?
解决方案
我找到了一种解决方案,它可能不是最优雅的解决方案。在摆弄了框架和其他可流动的东西之后,我重新阅读了文档,发现我可以将我的表格封装在另一个表格中以进行格式化。因此,我创建了一个单行两列的表,并将图像插入一列,另一表插入第二列。
col1 = Table([[im]])
col2 = Table(data, repeatRows=1)
tblrow1 = Table([[col1, col2]], colWidths=None)
tblrow1.setStyle(
TableStyle([
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
]))
elements.append(tblrow1)
推荐阅读
- java - Android上具有自定义颜色的布局中的圆角
- python - Termux:python脚本运行.exe时权限被拒绝
- reactjs - 反应引导宽度不适合移动屏幕
- python - C# 到 Python 的消息传递
- android - 从 VM 运行 fastboot 到本地设备
- sql - 在 oracle 11 的子查询中使用 rownum
- php - Laravel:如何在 Pivot belongsToMany 关系中获得我的朋友关系
- amazon-web-services - 数据传输费用是否适用于从 amazon S3 访问图像?
- apache-spark - Pyspark 获取最新值作为新列
- go - 如何将 mac 地址(十六进制)转换为 [3] 字节并将其传递给 ValidMACPrefixMap