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
- và
- và
Đố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 PDFTạ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ã saupdf = 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']
4Bố 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ề 0Vớ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 trangTham số
# Margin0 xác định vị trí sẽ đi sau ô này
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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 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
# Margin1 để chỉ định vị trí cụ thể cho phần tử tiếp theo hoặc phương thức
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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']
# Margin2 để tạo khoảng cách theo chiều dọc
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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']
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 = 50pdf = 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
# Margin3 và
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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']
# Margin4 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
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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']
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
7, bạn có thể đặt phông chữ, cỡ chữ và độ nhấn mạnh [thông thường, đậm, nghiêng]
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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'] - Trong phương thức
# Margin
8, bạn có thể xác định căn chỉnh văn bản với tham số
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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']# Margin
9
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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ô 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
0 và cũng xác định
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']# Custom class to overwrite the header and footer methods
1 trong phương thức
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']pdf = FPDF[]
5
pdf.add_page[]
pdf.output[f'./example.pdf', 'F'] - Để 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
3
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']
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ướiVớ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 methods5 thay thế, phương pháp này có thể xử lý ngắt dòng và ngắt trang
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']
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 = 8class 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 snsfig, 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 methods6
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']
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 methods9 và sử dụng hiệu quả tham số
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']
# Margin0, bạn có thể lặp qua DataFrame để tạo bảng
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*MARGIN
# Cell height
ch = 50pdf = 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']
pdf = FPDF[]
pdf.add_page[]
pdf.output[f'./example.pdf', 'F']
0Pandas 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ậnMặ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ụngBài viết này đã cho bạn thấy
- và
- và
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