0

I have been struggling in an attempt to change the paragraph style in report lab. I think the issue is probably a lack of understanding what classes are. If anyone could give me some pointers that would be awesome. Bellow is my code. When I run it I get the error 'Error when calling the metaclass bases init() takes at most 3 arguments (4 given)'.

Cheers,

Robin

from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import mm
from reportlab.platypus import Paragraph, Frame

Title = 'Test'

c  = Canvas(str(Title)+'.pdf')  

story = []

file = open('Acknowledgements.txt','r')
lis = []
for line in file:
    lis.append(line)





styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading1']

class ParagraphStyle(styleN):
           defaults = {
               'fontName':'Helvetica',
               'fontSize':14,
               'leading':12,
               'leftIndent':0,
               'rightIndent':0,
               'firstLineIndent':0,
               'alignment':0,
               'spaceBefore':0,
               'spaceAfter':0,
               'bulletFontName':'Helvetica',
               'bulletFontSize':10,
               'bulletIndent':0,
               'textColor': 'k',
               'backColor':None,
               'wordWrap':None,
               'borderWidth': 0,
               'borderPadding': 0,
               'borderColor': None,
               'borderRadius': None,
               'allowWidows': 1,
               'allowOrphans': 0,
               'textTransform':None,
               'endDots':None,
               'splitLongWords':1,
               'underlineProportion': 0,
               'bulletAnchor': 'start',
               'justifyLastLine': 0,
               'justifyBreaks': 0,
               'spaceShrinkage': 0,
               }




story.append(Paragraph('Acknowledgements', styleH))
for l in lis:
    story.append(Paragraph(l, styleN))



f = Frame(110*mm, 0*mm, 90*mm, 280*mm, showBoundary=0)

f.addFromList(story,c)

c.save()
Robin
  • 389
  • 5
  • 19
  • Full traceback? which specific line does it break on? – Gavin Achtemeier Jul 27 '17 at 05:22
  • Additionally, your question and question title are very specific for what seems to be a relatively generic error (though you may not see that). If you can make your question more generic that will allow other people to find it more easily. – Gavin Achtemeier Jul 27 '17 at 05:50

1 Answers1

0

Without debugging your code for you:

__init__() is a Class initializer. (It is similar to New in java)

What that means is: if I have defined a Class Foo somewhere, and later do a = Foo(param1), what happens in the background is Python allocates memory for the class then calls Foo.__init__(self, param1), where self is the newly allocated memory for the class.

Notice that Python inserted self into the parameters without you seeing it. This means that Foo.__init__() technically received two arguments (according to Python and any traceback) where you only enter one argument into Foo()! This can be a confusing tripup for those not familiar with classes in Python.

So in your case, you (or something you call) are initializing a class and passing it three arguments (IE: a = Foo(1, 2, 3)). Python then passes self followed by the parameters you entered to that class's __init__() so that it receives four arguments (self, 1, 2, 3). The class was declared with only three arguments, so you are passing one too many! It can only tolerate you passing two (or possibly less if it takes keyword args) arguments, so that when self is added __init__() gets three total arguments.

When this comes up it is usually best to check the line it failed on and make sure it looks okay, then refer to the documentation for the Class you are trying to initialize.

Gavin Achtemeier
  • 325
  • 2
  • 11
  • I think this may be getting closer but I now have the issue of color being undefined and throwing an error. Thanks for your help I think I am starting to understand classes. – Robin Jul 27 '17 at 07:03
  • https://stackoverflow.com/questions/9855445/how-to-change-text-font-color-in-reportlab-pdfgen help? – Gavin Achtemeier Jul 28 '17 at 01:41