I keep getting an error " no attribute for string .pdf" with my code. It works as a static filename, but not as a variable. I have read the other examples on opening a filename, but none of them are inline like reportlab needs.
testid = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
basename = str("cert")
filename = "_".join([basename, testid])
print filename
canvas = canvas.Canvas(filename.pdf, pagesize=letter)
Here's a working example that fails on the last line, but should demonstrate the problem:
#!python2
#coding=utf-8
import datetime
testid = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
basename = str("cert")
filename = "_".join([basename, testid])
print filename
print "filename.pdf"
print filename.pdf
Output:
cert_170508_152300
filename.pdf
Traceback (most recent call last):
File "test.py", line 11, in <module>
print filename.pdf
AttributeError: 'str' object has no attribute 'pdf'
You are passing a non-existing object(* as parameter, where a string is required.
(* actually, since filename exists (a string), it assumes you are trying to access its non-existing attribute (just like the error says).
This should work:
canvas = canvas.Canvas(filename, pagesize=letter)
This appends the .pdf suffix:
canvas = canvas.Canvas(filename + '.pdf', pagesize=letter)