from reportlab.pdfgen import canvas pdf = canvas.Canvas("example.pdf") pdf.drawString(100, 750, "Hello, World!") pdf.save()
from reportlab.lib.units import inch from reportlab.pdfgen import canvas pdf = canvas.Canvas("formatted.pdf") textobject = pdf.beginText() textobject.setTextOrigin(inch, 11*inch) textobject.textLines(''' Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ''') pdf.drawText(textobject) pdf.save()This code creates a new PDF file called "formatted.pdf" and adds formatted text at the location (1 inch, 11 inches) using the `beginText`, `setTextOrigin`, and `textLines` methods. It demonstrates how to use these methods to format text in a PDF document. In conclusion, the Python package library used for generating PDF files with text, images, and other graphical elements is called reportlab.pdfgen.canvas Canvas. The `drawString` method is used to add text to a PDF document, while other methods such as `beginText`, `setTextOrigin`, and `textLines` can be used to format text.