本文整理汇总了Python中reportlab.platypus.paragraph.Paragraph.wrap方法的典型用法代码示例。如果您正苦于以下问题:Python Paragraph.wrap方法的具体用法?Python Paragraph.wrap怎么用?Python Paragraph.wrap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reportlab.platypus.paragraph.Paragraph
的用法示例。
在下文中一共展示了Paragraph.wrap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test3
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def test3(self):
'''compare CJK splitting in some edge cases'''
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.enums import TA_LEFT
sty = ParagraphStyle('A')
sty.fontSize = 15
sty.leading = sty.fontSize*1.2
sty.fontName = 'Courier'
sty.alignment = TA_LEFT
sty.wordWrap = 'CJK'
p0=Paragraph('ABCDEFGHIJKL]N',sty)
p1=Paragraph('AB<font color="red">C</font>DEFGHIJKL]N',sty)
canv = Canvas('test_platypus_paragraph_cjk3.pdf')
ix = len(canv._code)
aW = pdfmetrics.stringWidth('ABCD','Courier',15)
w,h=p0.wrap(aW,1000000)
y = canv._pagesize[1]-72-h
p0.drawOn(canv,72,y)
w,h=p1.wrap(aW,1000000)
y -= h+10
p1.drawOn(canv,72,y)
w,h=p0.wrap(aW*0.25-2,1000000)
y -= h+10
p0.drawOn(canv,72,y)
w,h=p1.wrap(aW/4.-2,1000000)
y -= h+10
p1.drawOn(canv,72,y)
assert canv._code[ix:]==['q', '1 0 0 1 72 697.8898 cm', 'q', '0 0 0 rg', 'BT 1 0 0 1 0 57 Tm /F2 15 Tf 18 TL (ABCD) Tj T* (EFGH) Tj T* (IJKL]) Tj T* (N) Tj T* ET', 'Q', 'Q', 'q', '1 0 0 1 72 615.8898 cm', 'q', 'BT 1 0 0 1 0 57 Tm 18 TL /F2 15 Tf 0 0 0 rg (AB) Tj 1 0 0 rg (C) Tj 0 0 0 rg (D) Tj T* (EFGH) Tj T* (IJKL]) Tj T* (N) Tj T* ET', 'Q', 'Q', 'q', '1 0 0 1 72 353.8898 cm', 'q', '0 0 0 rg', 'BT 1 0 0 1 0 237 Tm /F2 15 Tf 18 TL (A) Tj T* (B) Tj T* (C) Tj T* (D) Tj T* (E) Tj T* (F) Tj T* (G) Tj T* (H) Tj T* (I) Tj T* (J) Tj T* (K) Tj T* (L) Tj T* (]) Tj T* (N) Tj T* ET', 'Q', 'Q', 'q', '1 0 0 1 72 91.88976 cm', 'q', 'BT 1 0 0 1 0 237 Tm 18 TL /F2 15 Tf 0 0 0 rg (A) Tj T* (B) Tj T* 1 0 0 rg (C) Tj T* 0 0 0 rg (D) Tj T* (E) Tj T* (F) Tj T* (G) Tj T* (H) Tj T* (I) Tj T* (J) Tj T* (K) Tj T* (L) Tj T* (]) Tj T* (N) Tj T* ET', 'Q', 'Q']
canv.showPage()
canv.save()
示例2: Figure
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
class Figure(Flowable):
def __init__(self, imgFile, captionTxt, captionStyle, imgWidth=None, imgHeight=None, margin=(0, 0, 0, 0),
padding=(0, 0, 0, 0), align=None, borderColor=(0.75, 0.75, 0.75), no_mask=False, url=None):
imgFile = imgFile
self.imgPath = imgFile
# workaround for http://code.pediapress.com/wiki/ticket/324
# see http://two.pairlist.net/pipermail/reportlab-users/2008-October/007526.html
if no_mask:
self.i = Image(imgFile, width=imgWidth, height=imgHeight, mask=None)
else:
self.i = Image(imgFile, width=imgWidth, height=imgHeight)
self.imgWidth = imgWidth
self.imgHeight = imgHeight
self.c = Paragraph(captionTxt, style=captionStyle)
self.margin = margin # 4-tuple. margins in order: top, right, bottom, left
self.padding = padding # same as above
self.borderColor = borderColor
self.align = align
self.cs = captionStyle
self.captionTxt = captionTxt
self.availWidth = None
self.availHeight = None
self.url = url
def draw(self):
canv = self.canv
if self.align == "center":
canv.translate((self.availWidth - self.width) / 2, 0)
canv.saveState()
canv.setStrokeColor(Color(self.borderColor[0], self.borderColor[1], self.borderColor[2]))
canv.rect(self.margin[3], self.margin[2], self.boxWidth, self.boxHeight)
canv.restoreState()
canv.translate(self.margin[3] + self.padding[3], self.margin[2] + self.padding[2] - 2)
self.c.canv = canv
self.c.draw()
canv.translate((self.boxWidth - self.padding[1] - self.padding[3] - self.i.drawWidth) / 2, self.captionHeight + 2)
self.i.canv = canv
self.i.draw()
if self.url:
frags = urlparse.urlsplit(self.url.encode('utf-8'))
clean_url = urlparse.urlunsplit((frags.scheme,
frags.netloc,
urllib.quote(frags.path, safe='/'),
urllib.quote(frags.query, safe='=&'),
frags.fragment,)).decode('utf-8')
canv.linkURL(clean_url, (0, 0, self.imgWidth, self.imgHeight), relative=1, thickness=0)
def wrap(self, availWidth, availHeight):
self.availWidth = availWidth
self.availHeight = availHeight
contentWidth = max(self.i.drawWidth, self.c.wrap(self.i.drawWidth, availHeight)[0])
self.boxWidth = contentWidth + self.padding[1] + self.padding[3]
(self.captionWidth, self.captionHeight) = self.c.wrap(contentWidth, availHeight)
self.captionHeight += self.cs.spaceBefore + self.cs.spaceAfter
self.boxHeight = self.i.drawHeight + self.captionHeight + self.padding[0] + self.padding[2]
self.width = self.boxWidth + self.margin[1] + self.margin[3]
self.height = self.boxHeight + self.margin[0] + self.margin[2]
return (self.width, self.height)
示例3: prepare_first_page
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def prepare_first_page(canvas, document):
p1 = Paragraph(presentation.title, styles['Heading'])
p2 = Paragraph(
presentation.owner.get_full_name(), styles['SubHeading'])
avail_width = width - inch
avail_height = height - inch
w1, h1 = p1.wrap(avail_width, avail_height)
w2, h2 = p2.wrap(avail_width, avail_height)
f = Frame(
inch / 2,
inch / 2,
width - inch,
height - inch,
leftPadding=0,
bottomPadding=0,
rightPadding=0,
topPadding=0
)
f.addFromList([p1, p2], canvas)
document.pageTemplate.frames[0].height -= h1 + h2 + inch / 2
document.pageTemplate.frames[1].height -= h1 + h2 + inch / 2
canvas.saveState()
canvas.setStrokeColorRGB(0, 0, 0)
canvas.line(
width / 2, inch / 2, width / 2, height - inch - h1 - h2)
canvas.restoreState()
示例4: beforeDrawPage
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def beforeDrawPage(self,canvas,doc):
canvas.setFont(serif_font,10)
canvas.setLineWidth(0)
#header
canvas.line(header_margin_hor, page_height - header_margin_vert, page_width - header_margin_hor, page_height - header_margin_vert )
if pdfstyles.show_page_header:
canvas.saveState()
canvas.resetTransforms()
canvas.translate(header_margin_hor, page_height - header_margin_vert - 0.1*cm)
p = Paragraph(self.title, text_style())
p.canv = canvas
p.wrap(page_width - header_margin_hor*2.5, page_height) # add an extra 0.5 margin to have enough space for page number
p.drawPara()
canvas.restoreState()
canvas.drawRightString(page_width - header_margin_hor, page_height - header_margin_vert + 0.1 * cm, "%d" % doc.page)
#Footer
canvas.saveState()
canvas.setFont(serif_font,8)
canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert )
if pdfstyles.show_page_footer:
p = Paragraph(formatter.cleanText(pagefooter, escape=False), text_style())
p.canv = canvas
w,h = p.wrap(page_width - header_margin_hor*2.5, page_height)
p.drawOn(canvas, footer_margin_hor, footer_margin_vert - 10 - h)
canvas.restoreState()
示例5: go
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def go():
styles = getSampleStyleSheet()
style=styles['Normal']
p1 = Paragraph('This is a paragraph', style )
print(p1.wrap(500,701))
print(p1._cache['avail'])
print(len(p1.split(500,701)))
print(p1.wrap(500,700))
print(len(p1.split(500,700)))
示例6: _draw_tyvek
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def _draw_tyvek(p, inventory):
p.line(15, 245, 345, 245)
p.line(15, 188, 345, 188)
p.line(15, 115, 345, 115)
p.setFontSize(12)
p.drawString(26, 290, 'DC-%0.3d' % inventory.dropship.id)
p.drawString(295, 290, str(inventory.item.id))
if len(inventory.item.name) > 55:
p.setFont('Helvetica-Bold', 8)
else:
p.setFont('Helvetica-Bold', 10)
p.drawString(26, 260, ellipsize(inventory.item.name, 75))
p.setFont('Helvetica-Bold', 10)
p.drawString( 26, 224, 'Console:')
p.drawString(120, 224, inventory.item.category.name)
p.drawString( 26, 200, 'Rating:')
p.drawString(120, 200, inventory.item.rating.title)
p.drawImage(inventory.item.rating.image.path, 211, 197, width=20, height=30)
p.setFont('Helvetica', 10)
styles = getSampleStyleSheet()
para = Paragraph(ellipsize(inventory.item.description, 305), styles['Normal'])
para.wrap(290, 10000)
i = 0
ll = para.breakLines(290)
for l in ll.lines:
if ll.kind == 0:
p.drawString(25, 170 - i, ' '.join(l[1]))
else:
p.drawString(25, 170 - i, ' '.join([x.text for x in l.words]))
i += 10
from code128 import Code128
bar = Code128()
image = bar.getImage(inventory.barcode, 50, "png")
_fd, n = tempfile.mkstemp()
image.save(n, "PNG")
p.drawImage(n, 196, 60, width=140, height=50)
p.setFont('Helvetica-Bold', 10)
p.drawString(236, 50, inventory.barcode)
p.drawImage(
os.path.join(settings.STATIC_ROOT, "img/bw-logo.png"),
26, 80, width=87, height=22)
p.setFont('Helvetica-Bold', 10)
p.drawString(25, 70, 'PO Box 6487')
p.drawString(25, 58, 'Delray Beach, FL 33482-9901')
p.showPage()
示例7: test2
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def test2(self):
sty = ParagraphStyle(name = 'normal')
sty.fontName = 'Times-Roman'
sty.fontSize = 10
sty.leading = 12
p = Paragraph('one two three',sty)
p.wrap(20,36)
self.assertEqual(len(p.split(20,24)),2) #widows allowed
self.assertEqual(len(p.split(20,16)),0) #orphans disallowed
p.allowWidows = 0
self.assertEqual(len(p.split(20,24)),0) #widows disallowed
p.allowOrphans = 1
self.assertEqual(len(p.split(20,16)),2) #orphans allowed
示例8: wrap
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def wrap(self, availWidth, availHeight):
# reduce the available width & height by the padding so the wrapping
# will use the correct size
style = self.style
availWidth -= style.paddingLeft + style.paddingRight
availHeight -= style.paddingTop + style.paddingBottom
# call the base class to do wrapping and calculate the size
Paragraph.wrap(self, availWidth, availHeight)
# increase the calculated size by the padding
self.width += style.paddingLeft + style.paddingRight
self.height += style.paddingTop + style.paddingBottom
return (self.width, self.height)
示例9: beforeDrawPage
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def beforeDrawPage(self,canvas,doc):
canvas.setFont(serif_font,8)
canvas.saveState()
if pdfstyles.show_title_page_footer:
canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert )
footertext = [_(titlepagefooter)]
if pdfstyles.show_creation_date:
footertext.append('PDF generated at: %s' % strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()))
p = Paragraph('<br/>'.join([formatter.cleanText(line, escape=False) for line in footertext]),
text_style(mode='footer'))
w,h = p.wrap(print_width, print_height)
canvas.translate( (page_width-w)/2.0, footer_margin_vert - h - 0.25*cm)
p.canv = canvas
p.draw()
canvas.restoreState()
if self.cover:
width, height = self._scale_img(pdfstyles.title_page_image_size, self.cover)
if pdfstyles.title_page_image_pos[0] == None:
x = (page_width - width) / 2.0
else:
x = max(0, min(page_width-width, pdfstyles.title_page_image_pos[0]))
if pdfstyles.title_page_image_pos[1] == None:
y = (page_height - height) / 2.0
else:
y = max(0, min(page_height-height, pdfstyles.title_page_image_pos[1]))
canvas.drawImage(self.cover, x, y, width , height)
示例10: beforeDrawPage
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def beforeDrawPage(self, canvas, doc):
canvas.setFont(serif_font, 8)
canvas.saveState()
if pdfstyles.show_title_page_footer:
canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert)
footertext = [_(titlepagefooter)]
if pdfstyles.show_creation_date:
locale.setlocale(locale.LC_ALL, "")
footertext.append(
pdfstyles.creation_date_txt % time.strftime(pdfstyles.creation_date_format, time.localtime())
)
lines = [formatter.cleanText(line, escape=False) for line in footertext]
txt = "<br/>".join(line if isinstance(line, unicode) else unicode(line, "utf-8") for line in lines)
p = Paragraph(txt, text_style(mode="footer"))
w, h = p.wrap(print_width, print_height)
canvas.translate((page_width - w) / 2.0, footer_margin_vert - h - 0.25 * cm)
p.canv = canvas
p.draw()
canvas.restoreState()
if self.cover:
width, height = self._scale_img(pdfstyles.title_page_image_size, self.cover)
if pdfstyles.title_page_image_pos[0] is None:
x = (page_width - width) / 2.0
else:
x = max(0, min(page_width - width, pdfstyles.title_page_image_pos[0]))
if pdfstyles.title_page_image_pos[1] is None:
y = (page_height - height) / 2.0
else:
y = max(0, min(page_height - height, pdfstyles.title_page_image_pos[1]))
canvas.drawImage(self.cover, x, y, width, height)
示例11: test3
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def test3(self):
from reportlab.pdfgen.canvas import Canvas
aW=307
styleSheet = getSampleStyleSheet()
bt = styleSheet['BodyText']
btj = ParagraphStyle('bodyText1j',parent=bt,alignment=TA_JUSTIFY)
p=Paragraph("""<a name='top'/>Subsequent pages test pageBreakBefore, frameBreakBefore and
keepTogether attributes. Generated at 1111. The number in brackets
at the end of each paragraph is its position in the story. llllllllllllllllllllllllll
bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc ddddddddddddddddddddd eeeeyyy""",btj)
w,h=p.wrap(aW,1000)
canv=Canvas('test_platypus_paragraph_just.pdf',pagesize=(aW,h))
i=len(canv._code)
p.drawOn(canv,0,0)
ParaCode=canv._code[i:]
canv.saveState()
canv.setLineWidth(0)
canv.setStrokeColorRGB(1,0,0)
canv.rect(0,0,aW,h)
canv.restoreState()
canv.showPage()
canv.save()
from reportlab import rl_config
x = rl_config.paraFontSizeHeightOffset and '50' or '53.17'
good = ['q', '1 0 0 1 0 0 cm', 'q', 'BT 1 0 0 1 0 '+x+' Tm 3.59 Tw 12 TL /F1 10 Tf 0 0 0 rg (Subsequent pages test pageBreakBefore, frameBreakBefore and) Tj T* 0 Tw .23 Tw (keepTogether attributes. Generated at 1111. The number in brackets) Tj T* 0 Tw .299167 Tw (at the end of each paragraph is its position in the story. llllllllllllllllllllllllll) Tj T* 0 Tw 66.9 Tw (bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc) Tj T* 0 Tw (ddddddddddddddddddddd eeeeyyy) Tj T* ET', 'Q', 'Q']
ok= ParaCode==good
assert ok, "\nParaCode=%r\nexpected=%r" % (ParaCode,good)
示例12: ReferenceText
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
class ReferenceText(IndexingFlowable):
"""Fakery to illustrate how a reference would work if we could
put it in a paragraph."""
def __init__(self, textPattern, targetKey):
self.textPattern = textPattern
self.target = targetKey
self.paraStyle = ParagraphStyle('tmp')
self._lastPageNum = None
self._pageNum = -999
self._para = None
def beforeBuild(self):
self._lastPageNum = self._pageNum
def notify(self, kind, stuff):
if kind == 'Target':
(key, pageNum) = stuff
if key == self.target:
self._pageNum = pageNum
def wrap(self, availWidth, availHeight):
text = self.textPattern % self._lastPageNum
self._para = Paragraph(text, self.paraStyle)
return self._para.wrap(availWidth, availHeight)
def drawOn(self, canvas, x, y, _sW=0):
self._para.drawOn(canvas, x, y, _sW)
示例13: prepare_first_page
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def prepare_first_page(canvas, document):
p1 = Paragraph(presentation.title, styles['Heading'])
p2 = Paragraph(presentation.owner.get_full_name(), styles['SubHeading'])
avail_width = width - inch
# TODO: determine if the complaint about height being undeclared is just pycharm or if its a problem
# if it is possibly a problem "it's better to be explicit" so refactor
avail_height = height - inch
w1, h1 = p1.wrap(avail_width, avail_height)
w2, h2 = p2.wrap(avail_width, avail_height)
f = Frame(inch / 2, inch / 2, width - inch, height - inch,
leftPadding=0, bottomPadding=0, rightPadding=0, topPadding=0)
f.addFromList([p1, p2], canvas)
document.pageTemplate.frames[0].height -= h1 + h2 + inch / 2
document.pageTemplate.frames[1].height -= h1 + h2 + inch / 2
canvas.saveState()
canvas.setStrokeColorRGB(0, 0, 0)
canvas.line(width / 2, inch / 2, width / 2, height - inch - h1 - h2)
canvas.restoreState()
示例14: HeaderFooter
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def HeaderFooter(canvas, doc):
canvas.saveState()
styleN = PS('nomal', fontName='Times-Roman', leading=9, fontSize=9)
P = Paragraph("This is a multi-line footer or header", styleN)
w, h = P.wrap(doc.width, doc.bottomMargin)
#print doc.width, doc.bottomMargin
#print w, h
#print dir(doc)
#print dir(canvas)
global pageNum
if doc.page < pageNum:
global ChapterName
ChapterName = 'Table of contents'
#print doc.page
#print ChapterName
pageNum = doc.page
if doc.page == 1:
footerMsg = []
footerMsg.append(Paragraph("Lei Yang", styleN))
footerMsg.append(Paragraph("Wei Gao", styleN))
footerMsg.append(Paragraph("XiangYu Dong", styleN))
footerMsg.append(Paragraph("Liang Chi", styleN))
footerMsg.append(Paragraph("Beijing ChaoYang, China", styleN))
canvas.line(2.5*cm, h+50, w+2.5*cm, h+50)
f = Frame(70, 2, 16*cm, 2.1*cm, showBoundary=0)
f.addFromList(footerMsg,canvas)
else:
P = Paragraph("User Guide", styleN)
w, h = P.wrap(doc.width, doc.bottomMargin)
P.drawOn(canvas, doc.leftMargin, h+789)
P = Paragraph(ChapterName, PS('nomal', fontName='Times-Roman', fontSize=9, alignment = TA_RIGHT, leading=9))
w, h = P.wrap(doc.width, doc.bottomMargin)
P.drawOn(canvas, doc.rightMargin, h+789)
P = Paragraph("Page %d" % doc.page, PS('nomal', fontName='Times-Roman', fontSize=9, alignment = 1))
w, h = P.wrap(doc.width, doc.bottomMargin)
P.drawOn(canvas, doc.leftMargin, h)
canvas.line(2.5*cm, h+780, w+2.5*cm, h+780)
canvas.restoreState()
示例15: test2
# 需要导入模块: from reportlab.platypus.paragraph import Paragraph [as 别名]
# 或者: from reportlab.platypus.paragraph.Paragraph import wrap [as 别名]
def test2(self):
'''CJK splitting in multi-frag case'''
style = ParagraphStyle('test', wordWrap = 'CJK')
p = Paragraph('bla <i>blub</i> '*130 , style)
aW,aH=439.275590551,121.88976378
w,h=p.wrap(aW,aH)
S=p.split(aW,aH)
assert len(S)==2, 'Multi frag CJK splitting failed'
w0,h0=S[0].wrap(aW,aH)
assert h0<=aH,'Multi-frag CJK split[0] has wrong height %s >= available %s' % (H0,aH)
w1,h1=S[1].wrap(aW,aH)
assert h0+h1==h, 'Multi-frag-CJK split[0].height(%s)+split[1].height(%s) don\'t add to original %s' % (h0,h1,h)