Python weasyprint Module: Comprehensive Guide from Installation to Advanced Use

Python weasyprint Module Guide

The WeasyPrint module is a powerful tool for converting HTML and CSS documents into PDF files. Built on top of Python, it is particularly useful for developers looking to generate dynamic reports, invoices, or any form of documents that require precise layouts and styling. This module is compatible with Python 3.5 and above, making it accessible for a wide range of projects.

Module Introduction

WeasyPrint is widely recognized for its ability to render HTML and CSS into PDF documents accurately. Instead of limiting you to basic formatting options, it supports advanced styling capabilities like CSS3 and complex layouts. It is ideal for users who want to automate document generation in their applications while maintaining high-quality styling and layout.

Application Scenarios

WeasyPrint can serve various purposes, including:

  • Generating PDFs for invoices, receipts, or reports in web applications.
  • Creating printable documents from HTML content such as tickets or brochures.
  • Converting web pages into PDF format for archiving or distribution.
    By leveraging WeasyPrint, developers can enhance user experience by providing downloadable formats of their content without sacrificing design aesthetics.

Installation Instructions

WeasyPrint is not part of the default Python library; thus, it requires separate installation. You can install it using pip, Python’s package installer.

Run the following command:

1
pip install WeasyPrint

Ensure you have the supporting dependencies installed for rendering fonts and page styles correctly, which may include additional libraries for your operating system.

Usage Examples

Example 1: Basic PDF Generation

1
2
3
4
5
6
from weasyprint import HTML  # Importing the HTML class from WeasyPrint

# Define an HTML string that we want to convert to PDF
html_content = '<h1>Hello, WeasyPrint!</h1><p>This is a PDF document.</p>'
# Create a PDF from the HTML content
HTML(string=html_content).write_pdf('output.pdf') # Writing the PDF to file

In this example, a simple HTML string containing a header and a paragraph is converted into a PDF file named ‘output.pdf’. This is useful for generating quick reports or documents.

Example 2: Using an HTML File

1
2
3
4
from weasyprint import HTML  # Importing the HTML class from WeasyPrint

# Load HTML content from a file
HTML('example.html').write_pdf('example_output.pdf') # Convert HTML file to PDF

By using an HTML file as input, we can easily generate PDFs from pre-styled content. This is crucial when transforming existing web pages into printable formats.

Example 3: Advanced Styling with CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from weasyprint import HTML  # Importing the HTML class from WeasyPrint

# Set up an HTML string with CSS styling
html_content = '''
<!DOCTYPE html>
<html>
<head>
<style>
h1 { color: blue; }
p { font-size: 12px; }
</style>
</head>
<body>
<h1>Styled Document</h1>
<p>This is a PDF with CSS styles applied!</p>
</body>
</html>
'''
# Convert the HTML string with CSS styling into a PDF
HTML(string=html_content).write_pdf('styled_output.pdf') # Writing the styled PDF to file

Here, we see how to incorporate CSS styles directly in an HTML document. This is particularly beneficial for developers wanting precise control over the appearance of their PDFs.

Software and library versions are constantly updated

If this document is no longer applicable or is incorrect, please leave a message or contact me for an update. Let's create a good learning atmosphere together. Thank you for your support! - Travis Tang

I strongly encourage everyone to follow my blog, EVZS Blog (全糖冲击博客), as it contains invaluable resources including tutorials on all the Python standard libraries for easy reference and learning. By subscribing, you’ll gain access to comprehensive materials that can enhance your coding skills, keep you updated on best practices, and allow you to explore advanced functionalities. Don’t miss out on the opportunity to learn and grow with a supportive community!