Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Reportlab标题超出可流动表格_Python 3.x_Reportlab - Fatal编程技术网

Python 3.x Reportlab标题超出可流动表格

Python 3.x Reportlab标题超出可流动表格,python-3.x,reportlab,Python 3.x,Reportlab,我正在尝试创建一个标题,它将出现在我用Reportlab生成的PDF文档的每个页面上。 我试图实现的是有一个由表组成的标题 我有一个方法可以创建如下标题: def make_header(self, canvas, doc): small_size = '8' big_size= '10' paragraphs = {} canvas.saveState() for key, name in (('doc_type', 'Document type

我正在尝试创建一个标题,它将出现在我用Reportlab生成的PDF文档的每个页面上。 我试图实现的是有一个由表组成的标题

我有一个方法可以创建如下标题:

    def make_header(self, canvas, doc):
    small_size = '8'
    big_size= '10'
    paragraphs = {}
    canvas.saveState()
    for key, name in (('doc_type', 'Document type'), ('owner', 'Owner'),
                      ('classification', 'Classification'),
                      ('document_nr', 'Document Number'),
                      ('date', 'Date'),
                      ('revision', 'Revision'),
                      ('title', 'Title')):
        t = self.header_info[key] if self.header_info[key] else ''
        paragraphs[key] = [Paragraph('<font size={}>{}</font>'.format(small_size, name), style=styles['Normal']),
                         Paragraph('<font size={}>{}</font>'.format(big_size, t), style=styles['Normal'])]

    im = self.scale_image("image.jpg", 10*cm)

    page = [Paragraph('<font size={}>{}</font>'.format(small_size, 'Page'), style=styles['Normal']),
            Paragraph('<font size={}>{}</font>'.format(big_size, doc.page), style=styles['Normal'])]

    t = Table([[im, paragraphs['doc_type'], paragraphs['owner'], paragraphs['date']],
               ['', paragraphs['classification'], paragraphs['document_nr'], paragraphs['revision']],
               ['', paragraphs['title'],'', page]])

    t.setStyle([('SPAN', (0,0), (0,2)),
                ('SPAN', (1,2), (2,2)),
                ('ALIGN', (0,0), (0,0), 'CENTER'),
                ('VALIGN', (0,0), (0,0), 'MIDDLE'),
                ('VALIGN', (1,0), (-1,-1), 'TOP')])
    self.table_grid(t)
    t.wrapOn(canvas, self.width-50, self.height)
    t.drawOn(canvas, *self.coord(10,10, mm))
    canvas.restoreState()
我的文档中唯一显示的是“test”字符串,而不是标题。 我已经使用print-in-make_头函数进行了测试,所以我知道它会被调用并运行


我找不到缺少的内容,请帮我找到正确的方向。

您好,您的问题与缺少的元素有点混淆。我认为问题在于你关于如何使用wrapOn和drawOn的想法。这是我的黑客版本,它至少做了些什么;有关reportlab的更多帮助和讨论,请访问用户列表

从reportlab.platypus导入间隔符、SimpleDoctTemplate、表格、表格样式
来自reportlab.platypus.paragration导入段落
从reportlab.lib.styles导入getSampleStyleSheet、ParagraphStyle
从reportlab.lib导入颜色
从reportlab.lib.units导入英寸
styles=getSampleStyleSheet()
课堂测试:
标题信息=dict(
文档类型='MMMMM',
)
def make_标题(自身、画布、文档):
小尺寸='8'
大尺寸='10'
段落={}
canvas.saveState()
pageWidth,pageHeight=画布。\u pagesize
对于键,在(('doc_type','Document type'),('owner','owner')中指定名称,
(‘分类’、‘分类’),
(“文件编号”、“文件编号”),
(“日期”、“日期”),
(‘修订’、‘修订’),
('title','title'):
t=self.header\u info.get(键“”)
段落[key]=[段落({}).格式(小尺寸,名称),样式=样式['Normal'],
段落({}.format(大尺寸,t),style=style['Normal'])]
#im=自缩放图像(“image.jpg”,10*cm)
页面=[段落({}).格式(小尺寸,'page'),样式=样式['Normal']),
段落(“{}”。格式(大尺寸,doc.page),style=style['Normal'])]
t=表格(['im',段落['doc_type',段落['owner',段落['date']],
['',第['分类]段,第['文件编号]段,第['修订]段,
['',段落['title','',第页]],
style=[('SPAN',(0,0),(0,2)),
(‘SPAN’,(1,2),(2,2)),
('ALIGN',(0,0),(0,0),'CENTER'),
('VALIGN',(0,0),(0,0),'MIDDLE'),
('VALIGN',(1,0),(-1,-1),'TOP'),
('INNERGRID',(0,0),(-1,-1),0.25,颜色。黑色),
('BOX',(0,0),(-1,-1),2,颜色。黑色),
])
#自选表格网格(t)
w、 h=t.wrapOn(画布,页宽-20,页高-20)
t、 绘图(帆布,10页,页高-h-10)
canvas.restoreState()
定义初始化(自):
self.doc=SimpleDocTemplate('stackoverflow-48184260.pdf')
def生成(自):
t=段落('test',style=样式['Normal'])
self.doc.build([t],onFirstPage=self.make_头)
如果“名称”=“\uuuuuuuu主要”:
测试=测试()
test.build()

感谢您的输入,正如您所说,在我使用wrapOn和drawOn方法时出现了错误。我知道如何在我的页面上绘制标题,不仅我必须将标题居中,而且我希望我能自己处理。我认为发生的是,由于我错误地使用了wrapOn方法,我试图在一个无法容纳标题的空间中渲染标题,因此它没有被Reportlab渲染并无声地丢弃,这是正确的推理吗?
def build(self):
    t = Paragraph('test', style=styles['Normal'])
    self.doc.build([t], onFirstPage=self.make_header)
from reportlab.platypus import Spacer, SimpleDocTemplate, Table, TableStyle
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch

styles = getSampleStyleSheet()
class Test:
    header_info = dict(
            doc_type='MMMMM',
            )
    def make_header(self, canvas, doc):
        small_size = '8'
        big_size= '10'
        paragraphs = {}
        canvas.saveState()
        pageWidth, pageHeight = canvas._pagesize

        for key, name in (('doc_type', 'Document type'), ('owner', 'Owner'),
                          ('classification', 'Classification'),
                          ('document_nr', 'Document Number'),
                          ('date', 'Date'),
                          ('revision', 'Revision'),
                          ('title', 'Title')):
            t = self.header_info.get(key,'')
            paragraphs[key] = [Paragraph('<font size={}>{}</font>'.format(small_size, name), style=styles['Normal']),
                             Paragraph('<font size={}>{}</font>'.format(big_size, t), style=styles['Normal'])]

        #im = self.scale_image("image.jpg", 10*cm)

        page = [Paragraph('<font size={}>{}</font>'.format(small_size, 'Page'), style=styles['Normal']),
                Paragraph('<font size={}>{}</font>'.format(big_size, doc.page), style=styles['Normal'])]

        t = Table([['im', paragraphs['doc_type'], paragraphs['owner'], paragraphs['date']],
                   ['', paragraphs['classification'], paragraphs['document_nr'], paragraphs['revision']],
                   ['', paragraphs['title'],'', page]],
                    style=[('SPAN', (0,0), (0,2)),
                    ('SPAN', (1,2), (2,2)),
                    ('ALIGN', (0,0), (0,0), 'CENTER'),
                    ('VALIGN', (0,0), (0,0), 'MIDDLE'),
                    ('VALIGN', (1,0), (-1,-1), 'TOP'),
                    ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                    ('BOX', (0,0), (-1,-1), 2, colors.black),
                    ])

        #self.table_grid(t)
        w,h = t.wrapOn(canvas, pageWidth-20, pageHeight-20)
        t.drawOn(canvas, 10, pageHeight-h-10)
        canvas.restoreState()


    def __init__(self):
        self.doc = SimpleDocTemplate('stackoverflow-48184260.pdf')

    def build(self):
        t = Paragraph('test', style=styles['Normal'])
        self.doc.build([t], onFirstPage=self.make_header)

if __name__=='__main__':
    test = Test()
    test.build()