Reputation: 3
This is my file structure:
main
|
| - generate.py
| - car.png
Inside the generate.py file, I'm building PDF document using ReportLab library, as follows:
def generatePDF():
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
doc = SimpleDocTemplate(base_dir)
story = []
# ... adding paragraphs of text
car = [['Renault', Image('car.png'), 'Description']]
table = Table(car, colWidths=100, rowHeights=50)
story.append(table)
doc.build(story)
Running this code raise the following error:
OSError at /car/
fileName='car.png' identity=[ImageReader@0x4563bb0 filename='car.png'] Cannot open resource "car.png"
Why is this? Can you see what I'm doing wrong here?
If I remove the image from table, everything works fine.
Upvotes: 0
Views: 458
Reputation: 148965
This is not a core Python error but it is raised by ReportLab. The error says that ReportLab cannot find the car.png
file. This is probably caused by the current directory not being what you expect. If you give a filename without a full path, the file is searched in the current working directory of the process, not in the directory of the Python file. You should use:
car = [['Renault', Image(os.path.join(base_dir, 'car.png')), 'Description']]
Upvotes: 1