ReportLab:如何从段落的左上点开始写作,因为默认情况下它是从左下点
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER, TA_RIGHT
pdfmetrics.registerFont(TTFont('Dejavu', 'DejaVuSansCondensed.ttf'
packet_1 = io.BytesIO()
can = canvas.Canvas(packet_1, pagesize=A4)
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
my_style = ParagraphStyle(name ='My style',
fontName='Dejavu',
fontSize=12,
alignment=TA_LEFT,
borderWidth = 1,
borderColor = 'black'
)
p3 = Paragraph('''abdc''', my_style)
p3.wrapOn(can, 500, 100)
p3.drawOn(can, 10, 300)
无论段落高度如何,文本都从底部开始。 如何使其从顶部开始?
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER, TA_RIGHT
pdfmetrics.registerFont(TTFont('Dejavu', 'DejaVuSansCondensed.ttf'
packet_1 = io.BytesIO()
can = canvas.Canvas(packet_1, pagesize=A4)
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
my_style = ParagraphStyle(name ='My style',
fontName='Dejavu',
fontSize=12,
alignment=TA_LEFT,
borderWidth = 1,
borderColor = 'black'
)
p3 = Paragraph('''abdc''', my_style)
p3.wrapOn(can, 500, 100)
p3.drawOn(can, 10, 300)
Regardless of the paragraph height, the text starts at the bottom.
How to make it start from the top?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
bottoRP = 0
exp:exp:add
bottomup=0
exp:免责声明我是
borb
的作者。BORB
是一个纯Python PDF库,既可以提供低级(您可以在哪个确切的坐标内容中使用)和高级(Borb
照顾布局)为您)模型。您可以查看示例,在
段落
对齐中有整个部分。您可以发现在这里。在示例中,
段落
在精确的坐标处添加,并使用各种Alignment
参数布置。我将在此处复制相关示例的完整性:
这应该产生以下PDF:
我在段落周围添加了一个红色矩形,以说明内容实际上是用
vertaltical_alignment
verticaltic_alignment < /code>设置为
alignment.top
。disclaimer I am the author of
borb
the library used in this answer.borb
is a pure Python PDF library that provides both a low-level (you decide at which exact coordinates content goes) and a high-level (borb
takes care of the layout for you) model.You can check out the examples, there's a whole section on
Paragraph
alignment. You can find that here.In the examples, a
Paragraph
is added at exact coordinates, and laid out with variousAlignment
parameters.I'll copy the relevant example here for completeness:
This should produce the following PDF:
I added a red rectangle around the Paragraph to illustrate that the content is in fact laid out with
vertical_alignment
set toAlignment.TOP
.