Hướng dẫn fpdf cell wrap text python - python văn bản bọc ô fpdf

Tôi đã tạo trên tệp PDF bằng mô -đun PYFPDF, nhưng tôi nhận thấy rằng văn bản không được căn chỉnh đúng trong mỗi hàng.

Tôi cần phải có bao bọc văn bản trong ô để điều chỉnh tự động. Tôi đã thử mã bên dưới để tạo tệp PDF có kích thước cố định, nhưng nó không hoạt động để trộn văn bản theo hàng, bạn có thể kiểm tra ảnh chụp màn hình trong nhận xét để xem vấn đề.

Tôi cần điều chỉnh kích thước cột bảng động. Xin vui lòng giúp đỡ.

mã số :


pdf = FPDF[]
pdf.add_page[]
pdf.set_font["Arial", size=8]

item_data = [['Item Name', 'Last clock', 'lastvalue'], ['cl Loaded Class Count', '0', '0'], ['cl Total Loaded Class Count', '0', '0'], ['cl Unloaded Class Count', '0', '0'], ['comp Name of the current JIT compiler', '0', ''], ['comp Accumulated time spent in compilation', '0', '0'], ['gc ConcurrentMarkSweep number of collections per second', '0', '0'], ['gc ConcurrentMarkSweep accumulated time spent in collection', '0', '0'], ['gc Copy number of collections per second', '0', '0'], ['gc Copy accumulated time spent in collection', '0', '0'], ['gc MarkSweepCompact number of collections per second', '0', '0'], ['gc MarkSweepCompact accumulated time spent in collection', '0', '0'], ['gc PS MarkSweep number of collections per second', '0', '0'], ['gc PS MarkSweep accumulated time spent in collection', '0', '0'], ['gc PS Scavenge number of collections per second', '0', '0'], ['gc PS Scavenge accumulated time spent in collection', '0', '0'], ['gc ParNew number of collections per second', '0', '0'], ['gc ParNew accumulated time spent in collection', '0', '0'], ['mem Heap Memory committed', '0', '0'], ['mem Heap Memory max', '0', '0'], ['mem Heap Memory used', '0', '0'], ['mem Non-Heap Memory committed', '0', '0'], ['mem Non-Heap Memory max', '0', '0'], ['mem Non-Heap Memory used', '0', '0'], ['mem Object Pending Finalization Count', '0', '0'], ['mp CMS Old Gen committed', '0', '0'], ['mp CMS Old Gen max', '0', '0'], ['mp CMS Old Gen used', '0', '0'], ['mp CMS Perm Gen committed', '0', '0'], ['mp CMS Perm Gen max', '0', '0'], ['mp CMS Perm Gen used', '0', '0'], ['mp Code Cache committed', '0', '0'], ['mp Code Cache max', '0', '0'], ['mp Code Cache used', '0', '0'], ['mp PS Old Gen committed', '0', '0'], ['mp PS Old Gen max', '0', '0'], ['mp PS Old Gen used', '0', '0'], ['mp PS Perm Gen committed', '0', '0'], ['mp PS Perm Gen max', '0', '0'], ['mp PS Perm Gen used', '0', '0'], ['mp Perm Gen committed', '0', '0'], ['mp Perm Gen max', '0', '0'], ['mp Perm Gen used', '0', '0'], ['mp Tenured Gen committed', '0', '0'], ['mp Tenured Gen max', '0', '0'], ['mp Tenured Gen used', '0', '0'], ['os Max File Descriptor Count', '0', '0'], ['os Open File Descriptor Count', '0', '0'], ['os Process CPU Load', '0', '0'], ['jvm Uptime', '0', '0'], ['jvm Name', '0', ''], ['jvm Version', '0', ''], ['th Daemon Thread Count', '0', '0'], ['th Peak Thread Count', '0', '0'], ['th Thread Count', '0', '0'], ['th Total Started Thread Count', '0', '0']]


col_width = pdf.w / 3.25
print[col_width]
row_height = pdf.font_size
for row in item_data:
    for item in row:
        spacing=1.5
        pdf.cell[col_width, row_height*spacing,txt=item, border=1]
    pdf.ln[row_height*spacing]
pdf.output["stack.pdf"]

fpdf.multi_cell[w: float, h: float, txt: str, border = 0, 
                align: str = 'J', fill: bool = False]

Sự mô tả

Phương pháp này cho phép in văn bản với ngắt dòng. Chúng có thể tự động [ngay khi văn bản đến đúng biên giới của ô] hoặc rõ ràng [thông qua ký tự \n]. Càng nhiều ô khi cần thiết là đầu ra, một bên dưới cái kia. Văn bản có thể được căn chỉnh, trung tâm hoặc hợp lý. Các khối tế bào có thể được đóng khung và nền được sơn.

Thông số

w:

Chiều rộng của các ô. Nếu 0, chúng mở rộng đến lề phải của trang.

h:

Chiều cao của các tế bào.

txt:

Chuỗi để in.

border:

Cho biết nếu đường viền phải được vẽ xung quanh khối ô. Giá trị có thể là một số:

  • 0: Không có biên giới
  • 1: Khung

hoặc một chuỗi chứa một số hoặc tất cả các ký tự sau [theo bất kỳ thứ tự nào]:

  • L: Trái
  • T: Top
  • R: Phải
  • B: Đáy

Giá trị mặc định: 0.

align:

Đặt căn chỉnh văn bản.Các giá trị có thể là:

  • L: Căn chỉnh trái
  • C: Trung tâm
  • R: Căn chỉnh đúng
  • fpdf.multi_cell[w: float, h: float, txt: str, border = 0, 
                    align: str = 'J', fill: bool = False]
    
    0: Biện minh [giá trị mặc định]

lấp đầy

Cho biết nếu nền tế bào phải được sơn [

fpdf.multi_cell[w: float, h: float, txt: str, border = 0, 
                align: str = 'J', fill: bool = False]
1] hoặc trong suốt [
fpdf.multi_cell[w: float, h: float, txt: str, border = 0, 
                align: str = 'J', fill: bool = False]
2].Giá trị mặc định: Sai.

Xem thêm

set_doc_option, set_font, set_draw_color, set_fill_color, set_text_color, set_line_width, cell, write, set_auto_page_break.

Bài Viết Liên Quan

Chủ Đề