Bienvenue sur IndexError.

Ici vous pouvez poser des questions sur Python et le Framework Django.

Consultez la FAQ pour améliorer vos chances d'avoir des réponses à vos questions.

comment utiliser namedtuple pour elaborer des parametres

0 votes

Pour utiliser reportlab pour faire des pdf j' ai ceci:

sample_style_sheet = getSampleStyleSheet()
filename = 'myfile.pdf'
pagesize = (140 * mm, 216 * mm)
topMargin=1*inch
leftMargin=1*inch
rightMargin=1*inch
bottomMargin=1*inch
SimpleDocTemplate(filename=filename,pagesize=pagesize,topMargin=topMargin,leftMargin=leftMargin,rightMargin=rightMargin,bottomMargin=bottomMargin)

comment avoir:

params = (filename='myfile.pdf',pagesize=pagesize,topMargin,leftMargin=leftMargin,rightMargin=rightMargin,bottomMargin=bottomMargin)

pour faire plus loin:

SimpleDocTemplate(params)

en utilisant:

from collections import namedtuple
fields = (topMargin, leftMargin, rightMargin, bottomMargin)
default_margin = 1*inch
Node = namedtuple('Node', fields, defaults=(default_margin,) * len(fields))
demandé 31-Aou-2019 par buffalo974 (2,942 points)
edité 31-Aou-2019 par buffalo974

1 Réponse

0 votes

SimpleDocTemplate(**params._asdict())

somenamedtuple._asdict()

Return a new dict which maps field names to their corresponding values

https://docs.python.org/3/library/collections.html#collections.namedtuple

répondu 31-Aou-2019 par ptank (370 points)

ben j'ai une erreur de syntaxe sur la 22:

params = (filename=name_file,pagesize=pagesize,Node)

from reportlab.platypus import PageBreak, SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.units import mm, inch
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfgen import canvas

from collections import namedtuple

sample_style_sheet = getSampleStyleSheet()

logo = "superlogo.png"
name_file = 'myfile__2.pdf'
pagesize = (140 * mm, 216 * mm)  
default_margin = 1*inch

fields = (topMargin, leftMargin, rightMargin, bottomMargin)
Node = namedtuple('Node', fields, defaults=(default_margin,) * len(fields))
params = (filename=name_file,pagesize=pagesize,Node)


styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))

my_doc = SimpleDocTemplate(**params._asdict())

im = Image(logo, 3*inch, 3*inch)

paragraph_1 = Paragraph("A title", sample_style_sheet['Heading1'])

paragraph_2 = Paragraph(
    "Some normal body text",
    sample_style_sheet['BodyText'])

paragraph_3 = Paragraph("Page 2 title", sample_style_sheet['Heading1'])

paragraph_4 = Paragraph(
    "Some normal body text for page 2",
    sample_style_sheet['BodyText'])

Story = []
Story.append(im)

Story.append(paragraph_1)
Story.append(paragraph_2)
Story.append(PageBreak())
Story.append(paragraph_3)
Story.append(paragraph_4)


ptext = '''
<seq>. </seq>Some Text<br/>
<seq>. </seq>Some more test Text
'''
Story.append(Paragraph(ptext, styles["Bullet"]))

ptext='<bullet>•</bullet>Some Text'
Story.append(Paragraph(ptext, styles["Bullet"]))


my_doc.build(Story)

Ton tuple params n'es pas un namedtuple donc pas d'assignation via = ni de méthode associé,
peut etre avant un :

Params = namedtuple("Params", ("filename", "pagesize", "node"))
Et du coup:

params = Param(filename=name_file, pagesize=pagesize, Node)

Oui je pense qu'on se rapproche mais ça doit faire un genre d'erreur d'unpacking sur Node à mon avis

fields = (topMargin, leftMargin, rightMargin, bottomMargin)
Node = namedtuple('Node', fields, defaults=(default_margin,) * len(fields))
Params = namedtuple("Params", ("filename", "pagesize", "node"))
params = Param(filename=name_file, pagesize=pagesize, Node)
                                                                                                       ^
SyntaxError: positional argument follows keyword argument
...