Bảng FPDF Python

Khi bạn đã hoàn thành việc phân tích dữ liệu của mình, bạn cần suy nghĩ về cách truyền đạt kết quả. Một phần của giao tiếp đó là quyết định định dạng tệp nào bạn sẽ cung cấp báo cáo phân tích dữ liệu của mình. Tôi chắc chắn rằng hầu hết các bên liên quan sẽ thích tệp PDF hơn Notebook iPython

Bài viết này sẽ thảo luận về cách bạn có thể tự động tạo PDF như một phần của quy trình phân tích dữ liệu, bao gồm

Đối với hướng dẫn này, chúng tôi sẽ sử dụng thư viện

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
1 [1]

from fpdf import FPDF

Mặc dù thư viện không được duy trì tích cực và chưa được cập nhật từ năm 2012 [1], nhưng nó rất dễ sử dụng cho hầu hết các trường hợp sử dụng. Nếu bạn có nhu cầu phức tạp hơn đối với việc tạo PDF của mình, bạn có thể xem các lựa chọn thay thế như PyPDF2, ReportLab hoặc WeasyPrint

Cách tạo tệp PDF

Tạo PDF với thư viện

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
1 [1] bằng Python rất đơn giản. Để tạo một tài liệu PDF trống, bạn tạo một thể hiện của lớp
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
3, thêm một trang trống và lưu nó với ba dòng mã sau

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']

Tài liệu PDF trống được tạo bằng ba dòng mã trong FPDF bằng Python [Hình ảnh của tác giả]

Trang mặc định là định dạng A4 ở chế độ dọc với lề 1cm. Bạn có thể xác định cấu hình trang tùy chỉnh với các tham số của phương thức

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
4

Bố cục và đặt văn bản

Tiếp theo, hãy hiểu bố cục của tài liệu

Bạn có thể thêm các ô văn bản bằng phương pháp

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
5. Để hiển thị bố cục kết quả, chúng tôi sẽ đặt tham số
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
6, hiển thị đường viền của ô. Khi đạt được thiết kế mong muốn, bạn có thể đặt giá trị tham số về 0

Với các tham số

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
7 và
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
8, bạn có thể xác định chiều rộng và chiều cao của ô văn bản. Một
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
9 sẽ dẫn đến một ô văn bản trải dài trên toàn bộ chiều rộng của trang

Tham số

# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']
0 xác định vị trí sẽ đi sau ô này

  • 0. ở bên phải của ô hiện tại
  • 1. đến đầu dòng tiếp theo
  • 2. bên dưới ô hiện tại

Để tạo khoảng cách giữa các ô, bạn có thể sử dụng phương thức

# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']
1 để chỉ định vị trí cụ thể cho phần tử tiếp theo hoặc phương thức
# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']
2 để tạo khoảng cách theo chiều dọc

Dưới đây bạn có thể xem một số ví dụ về các ô và bố cục của chúng. Bạn có thể tìm thấy mã tương ứng theo tên ô

# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']

Tạo bố cục PDF với các ô [Ảnh của tác giả]

Đầu trang và cuối trang

Bạn cũng có thể chỉ định đầu trang và chân trang được hiển thị trên mỗi trang trong tài liệu PDF. Đối với điều này, bạn cần ghi đè lên các phương thức

# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']
3 và
# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']
4 trong một lớp tùy chỉnh. Đừng quên sử dụng một thể hiện của lớp tùy chỉnh của bạn thay vì lớp
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
3

# Custom class to overwrite the header and footer methods
class PDF[FPDF]:

def __init__[self]:
super[].__init__[]
def header[self]:
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Header', 1, 1, 'C']
def footer[self]:
self.set_y[-15]
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Footer', 1, 0, 'C']
pdf = PDF[] # Instance of custom class
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
pdf.output[f'./example.pdf', 'F']

Đầu trang, Nội dung và Chân trang của tài liệu PDF được tạo bằng Python [Hình ảnh của tác giả]Cách thêm văn bản vào tệp PDF

Bây giờ bạn đã hiểu cách sắp xếp một tài liệu PDF, hãy điền vào các ô một số nội dung

Văn bản tạo kiểu

Thư viện

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
1 cung cấp cho bạn những điều cơ bản để tạo kiểu cho văn bản của bạn

  • Với phương pháp
    # Margin
    m = 10
    # Page width: Width of A4 is 210mm
    pw = 210 - 2*MARGIN
    # Cell height
    ch = 50
    pdf = FPDF[]
    pdf.add_page[]
    pdf.set_font['Arial', '', 12]
    pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
    pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
    pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
    pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
    pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
    pdf.output[f'./example.pdf', 'F']
    7, bạn có thể đặt phông chữ, cỡ chữ và độ nhấn mạnh [thông thường, đậm, nghiêng]
  • Trong phương thức
    # Margin
    m = 10
    # Page width: Width of A4 is 210mm
    pw = 210 - 2*MARGIN
    # Cell height
    ch = 50
    pdf = FPDF[]
    pdf.add_page[]
    pdf.set_font['Arial', '', 12]
    pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
    pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
    pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
    pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
    pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
    pdf.output[f'./example.pdf', 'F']
    8, bạn có thể xác định căn chỉnh văn bản với tham số
    # Margin
    m = 10
    # Page width: Width of A4 is 210mm
    pw = 210 - 2*MARGIN
    # Cell height
    ch = 50
    pdf = FPDF[]
    pdf.add_page[]
    pdf.set_font['Arial', '', 12]
    pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
    pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
    pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
    pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
    pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
    pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
    pdf.output[f'./example.pdf', 'F']
    9
  • Để tô nền cho ô, bạn cần xác định màu bằng phương thức
    # Custom class to overwrite the header and footer methods
    class PDF[FPDF]:

    def __init__[self]:
    super[].__init__[]
    def header[self]:
    self.set_font['Arial', '', 12]
    self.cell[0, 10, 'Header', 1, 1, 'C']
    def footer[self]:
    self.set_y[-15]
    self.set_font['Arial', '', 12]
    self.cell[0, 10, 'Footer', 1, 0, 'C']
    pdf = PDF[] # Instance of custom class
    pdf.add_page[]
    pdf.set_font['Arial', '', 12]
    pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
    pdf.output[f'./example.pdf', 'F']
    0 và cũng xác định
    # Custom class to overwrite the header and footer methods
    class PDF[FPDF]:

    def __init__[self]:
    super[].__init__[]
    def header[self]:
    self.set_font['Arial', '', 12]
    self.cell[0, 10, 'Header', 1, 1, 'C']
    def footer[self]:
    self.set_y[-15]
    self.set_font['Arial', '', 12]
    self.cell[0, 10, 'Footer', 1, 0, 'C']
    pdf = PDF[] # Instance of custom class
    pdf.add_page[]
    pdf.set_font['Arial', '', 12]
    pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
    pdf.output[f'./example.pdf', 'F']
    1 trong phương thức
    pdf = FPDF[]
    pdf.add_page[]
    pdf.output[f'./example.pdf', 'F']
    5
  • Để thay đổi màu văn bản của ô, bạn có thể xác định màu bằng phương thức
    # Custom class to overwrite the header and footer methods
    class PDF[FPDF]:

    def __init__[self]:
    super[].__init__[]
    def header[self]:
    self.set_font['Arial', '', 12]
    self.cell[0, 10, 'Header', 1, 1, 'C']
    def footer[self]:
    self.set_y[-15]
    self.set_font['Arial', '', 12]
    self.cell[0, 10, 'Footer', 1, 0, 'C']
    pdf = PDF[] # Instance of custom class
    pdf.add_page[]
    pdf.set_font['Arial', '', 12]
    pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
    pdf.output[f'./example.pdf', 'F']
    3
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 16]
pdf.cell[w=0, h=10, txt="This is regular text.", ln=1]
pdf.set_font['Arial', 'B', 16]
pdf.cell[w=0, h=10, txt="This is bold text.", ln=1]
pdf.set_font['Arial', 'I', 16]
pdf.cell[w=0, h=10, txt="This is italic text.", ln=1]
pdf.set_font['Arial', '', 16] # Reset text back to regularpdf.cell[w=0, h=10, txt="This is left aligned text.", ln=1,
align='L']
pdf.cell[w=0, h=10, txt="This is center aligned text.", ln=1,
align='C']
pdf.cell[w=0, h=10, txt="This is right aligned text.", ln=1,
align='R']
pdf.set_fill_color[r= 0, g= 128, b = 0]
pdf.cell[w=0, h=10, txt="This is text with filled background.", ln=1,
fill=True]
pdf.set_text_color[r= 0, g= 128, b = 0]
pdf.cell[w=0, h=10, txt="This is colored text.", ln=1]
pdf.output[f'./example.pdf', 'F']

Các kiểu văn bản khác nhau trong PDF được tạo. căn trái, căn giữa, căn phải, chữ đậm và chữ nghiêng, phông chữ và màu nền [Ảnh của tác giả]

Ngắt dòng và ngắt trang

Nếu bạn cần một khối văn bản dài hơn, phương pháp

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
5 là không đủ vì nó không cho phép ngắt dòng hoặc ngắt trang, như bạn có thể thấy bên dưới

Với mục đích này, bạn nên sử dụng phương pháp

# Custom class to overwrite the header and footer methods
class PDF[FPDF]:

def __init__[self]:
super[].__init__[]
def header[self]:
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Header', 1, 1, 'C']
def footer[self]:
self.set_y[-15]
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Footer', 1, 0, 'C']
pdf = PDF[] # Instance of custom class
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
pdf.output[f'./example.pdf', 'F']
5 thay thế, phương pháp này có thể xử lý ngắt dòng và ngắt trang

import lorem # Use this package to showcase long textspdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 16]
pdf.cell[w=0, h=50, txt="This and the below cells are regular cells." , border=1, ln=1]pdf.cell[w=0, h=50, txt="Example: " + lorem.text[], border=1, ln=1]pdf.multi_cell[w=0, h=50, txt="This and the below cells are multi cells.", border=1, ]pdf.multi_cell[w=0, h=5, txt="Example: " + lorem.text[], border=1, ]pdf.output[f'./example.pdf', 'F']

Sử dụng nhiều ô cho các văn bản dài hơn có ngắt dòng và ngắt trang thay vì các ô thông thường [Ảnh của tác giả]

Mẫu

Với mọi thứ bạn đã học cho đến nay, giờ đây bạn có thể tạo một mẫu đơn giản như mẫu được hiển thị bên dưới. Chúng tôi sẽ sử dụng điều này cho các ví dụ sau

# cell height
ch = 8
class PDF[FPDF]:
def __init__[self]:
super[].__init__[]
def header[self]:
self.set_font['Arial', '', 12]
self.cell[0, 8, 'Header', 0, 1, 'C']
def footer[self]:
self.set_y[-15]
self.set_font['Arial', '', 12]
self.cell[0, 8, f'Page {self.page_no[]}', 0, 0, 'C']
pdf = PDF[]
pdf.add_page[]
pdf.set_font['Arial', 'B', 24]
pdf.cell[w=0, h=20, txt="Title", ln=1]
pdf.set_font['Arial', '', 16]
pdf.cell[w=30, h=ch, txt="Date: ", ln=0]
pdf.cell[w=30, h=ch, txt="01/01/2022", ln=1]
pdf.cell[w=30, h=ch, txt="Author: ", ln=0]
pdf.cell[w=30, h=ch, txt="Max Mustermann", ln=1]
pdf.ln[ch]
pdf.multi_cell[w=0, h=5, txt=lorem.paragraph[]]
pdf.ln[ch]
pdf.multi_cell[w=0, h=5, txt=lorem.paragraph[]]
pdf.output[f'./example.pdf', 'F']

Mẫu PDF được tạo bằng Python [Hình ảnh của tác giả]

Đối với các ví dụ sau, chúng tôi sẽ sử dụng một tập dữ liệu hư cấu nhỏ

import pandas as pddf = pd.DataFrame[
{'feature 1' : ['cat 1', 'cat 2', 'cat 3', 'cat 4'],
'feature 2' : [400, 300, 200, 100]
}]

Tập dữ liệu hư cấu được nhập dưới dạng pandas DataFrame [Hình ảnh của tác giả] Cách thêm các ô Matplotlib dưới dạng hình ảnh vào tệp PDF

Ngoài văn bản, bạn có thể cần thêm các ô vào báo cáo PDF của mình

Để thêm các ô vào báo cáo PDF của bạn, trước tiên bạn cần lưu các ô Matplotlib của mình dưới dạng hình ảnh [e. g. , tệp PNG]

import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots[1,1, figsize = [6, 4]]sns.barplot[data = df, x = 'feature 1', y = 'feature 2']
plt.title["Chart"]
plt.savefig['./example_chart.png',
transparent=False,
facecolor='white',
bbox_inches="tight"]

Biểu đồ Matplotlib được lưu dưới dạng tệp PNG [Hình ảnh của tác giả]

Sau khi sơ đồ Matplotlib của bạn được lưu dưới dạng hình ảnh, bạn có thể thêm nó vào báo cáo bằng phương pháp

# Custom class to overwrite the header and footer methods
class PDF[FPDF]:

def __init__[self]:
super[].__init__[]
def header[self]:
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Header', 1, 1, 'C']
def footer[self]:
self.set_y[-15]
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Footer', 1, 0, 'C']
pdf = PDF[] # Instance of custom class
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
pdf.output[f'./example.pdf', 'F']
6

pdf = PDF[]
pdf.add_page[]
pdf.set_font['Arial', 'B', 24]
pdf.cell[w=0, h=20, txt="Title", ln=1]
pdf.set_font['Arial', '', 16]
pdf.cell[w=30, h=ch, txt="Date: ", ln=0]
pdf.cell[w=30, h=ch, txt="01/01/2022", ln=1]
pdf.cell[w=30, h=ch, txt="Author: ", ln=0]
pdf.cell[w=30, h=ch, txt="Max Mustermann", ln=1]
pdf.ln[ch]
pdf.multi_cell[w=0, h=5, txt=lorem.paragraph[]]
pdf.image['./example_chart.png',
x = 10, y = None, w = 100, h = 0, type = 'PNG']
pdf.ln[ch]
pdf.multi_cell[w=0, h=5, txt=lorem.paragraph[]]
pdf.output[f'./example.pdf', 'F']

Biểu đồ Matplotlib được thêm vào báo cáo PDF bằng Python [Hình ảnh của tác giả] Cách thêm Khung dữ liệu Pandas dưới dạng Bảng vào Tệp PDF

Thật không may, không có cách đơn giản nào để thêm DataFrame của gấu trúc vào báo cáo PDF bằng thư viện

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
3. Mặc dù việc thêm một Khung dữ liệu gấu trúc làm bảng vào báo cáo PDF yêu cầu một số mã hóa nhẹ, nhưng điều đó cũng không khó. Bằng cách sử dụng phương pháp
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
5 với
# Custom class to overwrite the header and footer methods
class PDF[FPDF]:

def __init__[self]:
super[].__init__[]
def header[self]:
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Header', 1, 1, 'C']
def footer[self]:
self.set_y[-15]
self.set_font['Arial', '', 12]
self.cell[0, 10, 'Footer', 1, 0, 'C']
pdf = PDF[] # Instance of custom class
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=255, txt = "Body", border = 1, ln = 1, align = 'C']
pdf.output[f'./example.pdf', 'F']
9 và sử dụng hiệu quả tham số
# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50
pdf = FPDF[]
pdf.add_page[]
pdf.set_font['Arial', '', 12]
pdf.cell[w=0, h=ch, txt="Cell 1", border=1, ln=1]pdf.cell[w=[pw/2], h=ch, txt="Cell 2a", border=1, ln=0]
pdf.cell[w=[pw/2], h=ch, txt="Cell 2b", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3a", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3b", border=1, ln=0]
pdf.cell[w=[pw/3], h=ch, txt="Cell 3c", border=1, ln=1]
pdf.cell[w=[pw/3], h=ch, txt="Cell 4a", border=1, ln=0]
pdf.cell[w=[pw/3]*2, h=ch, txt="Cell 4b", border=1, ln=1]
pdf.set_xy[x=10, y= 220] # or use pdf.ln[50]
pdf.cell[w=0, h=ch, txt="Cell 5", border=1, ln=1]
pdf.output[f'./example.pdf', 'F']
0, bạn có thể lặp qua DataFrame để tạo bảng

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
0

Pandas DataFrame được thêm vào báo cáo PDF dưới dạng bảng trong Python [Hình ảnh của tác giả]

Về mặt kỹ thuật, bạn cũng có thể chuyển đổi Khung dữ liệu gấu trúc của mình thành bảng Matplotlib, lưu nó dưới dạng hình ảnh và chèn bảng dưới dạng hình ảnh vào PDF. Nhưng tôi đã thử điều này, vì vậy bạn không cần phải. Nó không đẹp lắm

Phần kết luận

Mặc dù các nhà phê bình nói rằng có những lựa chọn thay thế tốt hơn cho thư viện

pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
1, nhưng nó rất đơn giản để sử dụng

Bài viết này đã cho bạn thấy

Bên dưới, bạn có thể sao chép mã mẫu tạo tệp PDF sau và điều chỉnh nó cho mục đích của mình

Rất thích câu chuyện này?

Nếu bạn muốn nhận những câu chuyện mới của tôi trực tiếp vào hộp thư đến của bạn, hãy đăng ký

Trở thành thành viên Medium để đọc thêm những câu chuyện từ các nhà văn khác và tôi. Bạn có thể hỗ trợ tôi bằng cách sử dụng liên kết giới thiệu của tôi khi bạn đăng ký. Tôi sẽ nhận được hoa hồng mà không tính thêm phí cho bạn

Fpdf trong Python là gì?

FPDF là gì? . Nếu chúng ta nói về tính năng thì chúng ta có thể tùy chỉnh như chúng ta có thể sửa đổi định dạng trang, lề, đơn vị, ngắt dòng tự động, v.v. a library in Python, it is used for pdf file document generation. If we talk about the feature then we can customize like we can modify the page format, margin, unit, automatic line break and more.

Làm cách nào để tạo bảng trong Fpdf bằng PHP?

yêu cầu php ['fpdf. $điền; . [hàng nghìn]'];

fpdf2 là gì?

fpdf2 là thư viện để tạo tài liệu PDF đơn giản và nhanh chóng bằng Python . Nó là một nhánh rẽ và là sự kế thừa của PyFPDF [cf. lịch sử].

EPW trong Fpdf là gì?

# Chiều rộng trang hiệu quả hoặc chỉ epw.

Chủ Đề