我有一个新手问题(应该很容易回答)在过去的一天左右一直困扰着我,在倾注了与reportlab相关的所有在线帖子之后仍然无法弄明白。我想要做的就是在同一个文档中从横向切换到纵向。这将允许我创建一个功能表(包括表格中这些图片的链接),然后放入图片,以便链接到。
这是我到目前为止提出的代码,以促进应该是一个简单的过程:
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, landscape
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Frame,
Table, TableStyle, NextPageTemplate, PageTemplate, BaseDocTemplate
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus.flowables import Flowable, PageBreak
from pyPdf import PdfFileReader, PdfFileWriter
import glob, os
from reportlab.lib.pagesizes import letter, A4
doc = BaseDocTemplate("M:\GIS Mapping Services\Promotional\Automated Reports\PDF_ReportLab\NextPageExample.pdf",showBoundary=1,pagesize=landscape(letter))
elements = []
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='normal', fontSize=6, leading = 7, alignment=TA_LEFT))
ptemplate = PageTemplate(id='portrait', pagesize=portrait(letter))
elements.append(Paragraph("Table is here.",styles["normal"]))
elements.append(NextPageTemplate('portrait'))
elements.append(PageBreak())
elements.append(Paragraph("Pictures are to be placed here.",styles["normal"]))
doc.build(elements)
del elements
如果您有任何提示或想帮助我,请具体说明一下我到底做错了什么,或者包括一个从头到尾没有任何编码缺失的工作示例。
提前感谢您的帮助,
温
答案 0 :(得分:7)
基于我找到here的答案,这是一个黑客攻击。
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, landscape
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Frame,Table, TableStyle, NextPageTemplate, PageTemplate, BaseDocTemplate
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus.flowables import Flowable, PageBreak
from pyPdf import PdfFileReader, PdfFileWriter
import glob, os
from reportlab.lib.pagesizes import letter, A4
def make_portrait(canvas,doc):
canvas.setPageSize(letter)
def make_landscape(canvas,doc):
canvas.setPageSize(landscape(letter))
doc = BaseDocTemplate("NextPageExample.pdf",showBoundary=1)
elements = []
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='normal', fontSize=6, leading = 7, alignment=TA_LEFT))
frame1 = Frame(doc.leftMargin, doc.height-5*inch,
doc.width, 5*inch,
leftPadding = 0, rightPadding = 0,
topPadding = 0, bottomPadding = 0,
id='frame1')
ptemplate = PageTemplate(id='portrait',frames =[frame1], onPage=make_portrait)
ltemplate = PageTemplate(id='landscape',frames =[frame1], onPage=make_landscape)
doc.addPageTemplates([ptemplate, ltemplate])
elements.append(Paragraph("Table is here.",styles["normal"]))
elements.append(NextPageTemplate('landscape'))
elements.append(PageBreak())
elements.append(Paragraph("Pictures are to be placed here.",styles["normal"]))
doc.build(elements)
del elements
使用platypus时,我发现操作画布的最简单方法是通过PageTemplate的onPage设置,它允许您实现文本的非流动部分。在此处,在横向或纵向模板上绘制任何内容之前,画布将重新调整为所需大小。
答案 1 :(得分:4)
您可以进一步简化上述内容。设置旋转画布不需要另外两个函数:
p_frame = Frame(0.5 * inch, 0.5 * inch, 7.5 * inch, 10 * inch,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='portrait_frame')
l_frame = Frame(0.5 * inch, 0.5 * inch, 10 * inch, 7.5 * inch,
leftPadding=0, rightPadding=0,
topPadding=0, bottomPadding=0,
id='landscape_frame')
portrait_tmpl = PageTemplate(id='portrait_tmpl', frames=[p_frame], pagesize=letter)
landscape_tmpl = PageTemplate(id='landscape_tmpl', frames=[l_frame], pagesize=landscape(letter))
它更易于维护。虽然,我还没有找到一种方法来设置两个独立的框架。
答案中的框架在轮换时对我不起作用。