【发布时间】:2021-05-30 01:11:44
【问题描述】:
我一直在尝试使用reportlab 生成表格,但在使用TableStyle 中的VALIGN 命令时遇到了问题。它似乎没有任何效果。每当我创建表格时,文本都会与网格线重叠,VALIGN 命令对文本的位置没有影响。下面是一个重现此效果的玩具示例。下图显示了重叠的文本。
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, Frame, Table, Spacer, TableStyle
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import inch
from datetime import datetime
from random import random
from reportlab.pdfgen import canvas
c = Canvas('L:/table_test.pdf', pagesize=letter, bottomup=False)
width, height = letter
CENTER_X = width/2
#c.translate(inch, inch) # Set origin
c.setFont("Times-Roman", 12) # Perfecting font
# Draw a shape
c.setStrokeColorRGB(0, 0, 0)
c.setFillColorRGB(0, 0, 0)
rand_data = [[round(random(), 2) for x in range(10)] for k in range(10)]
t = Table(rand_data)
t.setStyle(TableStyle([("BOX", (0,0), (-1, -1), 0, colors.black),
('INNERGRID', (0,0), (-1,-1), 0, colors.black),
('VALIGN',(0,0),(-1,-1),'BOTTOM'),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('FONT',(0,-1), (-1,-1), 'Times-Bold'),]))
t.wrapOn(c, 0, 0)
t.drawOn(c, 1*inch, 1.5*inch)
c.showPage()
c.save()
【问题讨论】: