I have written the following code, and have given the latex commands for drawing background, by using the NoEscape module.
I have an image reportbg.png in the same directory as the program. Now, I want this image to appear as a background in all pages of the report.
types = ('../Faults/*.png', '../Faults/*.jpg')
imgnames = []
for files in types:
imgnames.extend(natsort.natsorted(glob.glob(files)))
geometry_options = { "head": "30pt",
"margin": "0.3in",
"top": "0.2in",
"bottom": "0.4in",
"includeheadfoot": True}
doc = Document(geometry_options=geometry_options)
first_page = PageStyle("firstpage")
doc.preamble.append(first_page)
doc.change_document_style("firstpage")
new_comm1 = NoEscape(r'\usepackage{wallpaper}')
doc.append(new_comm1)
new_comm2 = NoEscape(r'\CenterWallPaper{reportbg.png}')
doc.append(new_comm2)
with doc.create(Section('Faults identified')):
doc.append("Report")
with doc.create(Subsection('Fault pictures')):
for i,imgname in enumerate(imgnames):
with doc.create(Figure(position='h!')) as f_pic:
f_pic.add_image(imgname, width='220px')
f_pic.add_caption('Height: '+str(56)+', Angle: '+str(20))
doc.append('Some regular text')
However, I got the following error:
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.23 \usepackage
{wallpaper}%
! Undefined control sequence.
l.24 \CenterWallPaper
{reportbg.png}%
<../Faults/1.jpg, id=1, 1927.2pt x 1084.05pt> <use ../Faults/1.jpg>
<../Faults/2.jpg, id=2, 1927.2pt x 1084.05pt> <use ../Faults/2.jpg>
<../Faults/3.jpg, id=3, 1927.2pt x 1084.05pt> <use ../Faults/3.jpg>
<../Faults/4.jpg, id=4, 1003.75pt x 1003.75pt> <use ../Faults/4.jpg>
LaTeX Warning: '!h' float specifier changed to '!ht'.
To implement a Background Image on all the pages of the document, you can generate first the PDF document in pylatex and then add the image as a watermark with PyPDF2. To do so, you need to have your 'reportbg.png' image into a pdf format (reportbg.pdf).
Here's a modified example based on the pylatex documentation (https://jeltef.github.io/PyLaTeX/current/examples/basic.html):
CODE
PS: The watermark, the initial generated pdf and the .py file must be at the same directory. I couldn't upload the PDF files, because this is my first answer post and I'm not really sure how could I, but I share some images. I hope it will be helpful. For more information, I suggest to read the following book: "Automate the boring stuff with Python", chapter 13, by Al Sweigart.