gpt4 book ai didi

python - 使用 ReportLab 旋转文档(竖排文本)

转载 作者:太空狗 更新时间:2023-10-29 22:27:19 26 4
gpt4 key购买 nike

我正在尝试获取类似于以下代码片段的内容来实际绘制旋转 90 度的文档。 pagesize最终已经是我想要的样子了,但是文字还是水平的。如何旋转文本使其垂直?

style = getSampleStyleSheet()
normal = style["Normal"]
normal.alignment = TA_CENTER
normal.fontName = "Helvetica"
normal.fontSize = 15

pdf = SimpleDocTemplate("testplatypus.pdf", pagesize = (80, 250), rightMargin=10, leftMargin=10, topMargin=20,bottomMargin=20)
story = []
text = "I really need this to be wrapped and vertical!"

para = Paragraph(text, normal)
story.append(para)
pdf.build(story)

最佳答案

这是这个问题的最高谷歌结果,所以我想我会发布一个更好的解决方案。我的回答直接来自 here

如果您使用的是文档模板,则可以使用非常轻量级的 Flowable,它将在特定表格单元格中创建垂直文本。

# rotatedtext.py
from reportlab.platypus.flowables import Flowable


class verticalText(Flowable):

'''Rotates a text in a table cell.'''

def __init__(self, text):
Flowable.__init__(self)
self.text = text

def draw(self):
canvas = self.canv
canvas.rotate(90)
fs = canvas._fontsize
canvas.translate(1, -fs/1.2) # canvas._leading?
canvas.drawString(0, 0, self.text)

def wrap(self, aW, aH):
canv = self.canv
fn, fs = canv._fontname, canv._fontsize
return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)

然后在文档中使用它:

# vertical_text_table.py
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib import colors
from reportlab.lib.colors import HexColor
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import (
BaseDocTemplate, Frame, Paragraph, NextPageTemplate,
PageBreak, PageTemplate, Image, Table, TableStyle, Spacer)
from rotatedtext import verticalText

document = BaseDocTemplate(
'Vertical.pdf')

Elements = []

titleFrame_1 = Frame(
0.5*inch, 0.75*inch, 7*inch, 9*inch, id='col1', showBoundary=0)
titleTemplate_1 = PageTemplate(
id='OneCol', frames=titleFrame_1)
document.addPageTemplates([titleTemplate_1])

cw = [1.2*inch] + [1*inch]*6
rh = [0.25*inch] + [.6*inch] + [0.25*inch]*7

data = [
['Some', 'Reporting', '', 'Data', '', 'Here', ''],
['', verticalText('Vertical'), verticalText('Text'),
verticalText('Vertical'), verticalText('Text'),
verticalText('Vertical'), verticalText('Text')],
['Row1', '0', '0', '69', '803', '20751', '11627'],
['Row2', '0', '0', '1', '0', '1096', '103'],
['Row3', '0', '0', '0', '0', '233', '1'],
['Row4', '0', '0', '0', '0', '694', '38'],
['Row5', '0', '0', '23', '2', '1319', '2'],
['Row6', '0', '0', '0', '0', '0', '0'],
['TOTAL', '0', '0', '93', '805', '24093', '11771'],
]

ts = [
('GRID', (0, 0), (-1, -1), 0.5, colors.black),
('SPAN', (1, 0), (2, 0)),
('SPAN', (3, 0), (4, 0)),
('SPAN', (5, 0), (6, 0)),
('SPAN', (0, 0), (0, 1)),
('ALIGN', (0, 0), (-1, 1), 'CENTER'),
('ALIGN', (0, 2), (-1, -1), 'RIGHT'),
('VALIGN', (0, 0), (-1, -2), 'MIDDLE'),
('FONT', (0, 0), (-1, 1), 'Helvetica-Bold', 7, 7),
('FONT', (0, 2), (0, -2), 'Helvetica-Bold', 7, 7),
('FONT', (1, 2), (-1, -2), 'Helvetica', 7, 7),
('FONT', (0, -1), (-1, -1), 'Helvetica-Bold', 8, 8),
('TEXTCOLOR', (0, -1), (-1, -1), colors.white),
('BACKGROUND', (0, -1), (-1, -1), colors.black),
]

t = Table(
data, style=ts,
colWidths=cw, rowHeights=rh)

Elements.append(t)
document.build(Elements)

给出:

Table with vertical text.

关于python - 使用 ReportLab 旋转文档(竖排文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com