Как добавить фоновое изображение страницы в pylatex? - PullRequest
0 голосов
/ 16 мая 2018

Я написал следующий код и дал латексные команды для рисования фона, используя модуль NoEscape.

У меня есть изображение reportbg.png в том же каталоге, что и программа. Теперь я хочу, чтобы это изображение отображалось в качестве фона на всех страницах отчета.

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')

Однако я получил следующую ошибку:

! 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'.

1 Ответ

0 голосов
/ 27 августа 2018

Чтобы реализовать фоновое изображение на всех страницах документа, вы можете сначала сгенерировать PDF-документ в pylatex, а затем добавить изображение в виде водяного знака с PyPDF2.Для этого вам нужно иметь изображение 'reportbg.png' в формате pdf (reportbg.pdf).

Вот модифицированный пример, основанный на документации Pylatex (https://jeltef.github.io/PyLaTeX/current/examples/basic.html):

) КОД

from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape
import PyPDF2

class Document_Watermark():

    def __init__(self, doc):
        self.doc = doc
        self.fill_document()
        self.create_document()
        self.Watermark()

    def fill_document(self):
        """Add a section, a subsection and some text to the document.

        :param doc: the document
        :type doc: :class:`pylatex.document.Document` instance
        """
        with self.doc.create(Section('A section')):
            self.doc.append('Some regular text and some ')
            self.doc.append(italic('italic text. '))

            with self.doc.create(Subsection('A subsection')):
                self.doc.append('Also some crazy characters: $&#{}')
    def create_document(self):
        # Add stuff to the document
        with self.doc.create(Section('A second section')):
            self.doc.append('Some text.')

        self.doc.generate_pdf('basic_maketitle2', clean_tex=False, compiler='pdflatex')
        tex = self.doc.dumps()  # The document as string in LaTeX syntax
    def Watermark(self):
        Doc = open('basic_maketitle2.pdf', 'rb')
        pdfReader = PyPDF2.PdfFileReader(Doc)
        pdfWatermark = PyPDF2.PdfFileReader(open('watermark3.pdf', 'rb'))
        pdfWriter = PyPDF2.PdfFileWriter()

        for pageNum in range(0, pdfReader.numPages):
            pageObj = pdfReader.getPage(pageNum)
            pageObj.mergePage(pdfWatermark.getPage(0))
            pdfWriter.addPage(pageObj)
            resultPdfFile = open('PDF_Watermark.pdf', 'wb')
            pdfWriter.write(resultPdfFile)
        Doc.close()
        resultPdfFile.close()



# Basic document
doc = Document('basic')


# Document with `\maketitle` command activated
doc = Document()

doc.preamble.append(Command('title', 'Awesome Title'))
doc.preamble.append(Command('author', 'Anonymous author'))
doc.preamble.append(Command('date', NoEscape(r'\today')))
doc.append(NoEscape(r'\maketitle'))

Document_Watermark(doc)

PS: водяной знак, исходный сгенерированный pdf и файл .py должны быть натот же каталог. Я не смог загрузить файлы PDF, потому что это мой первый пост с ответом, и я не совсем уверен, как это сделать, но я делюсь некоторыми изображениями. Надеюсь, это будет полезно. Для получения дополнительной информации я предлагаюпрочитать следующую книгу: «Автоматизируйте скучные вещи с помощью Python», глава 13, Аль Суигарт.

...