Hướng dẫn fpdf output python - python đầu ra fpdf

Tôi mới sử dụng Python nhưng đã gặp phải một vấn đề. Dựa trên bảng .csv tôi muốn tạo tệp pdf Based on .csv table i would like to create a pdf file
Based on .csv table i would like to create a pdf file

customer_iditem_idsố lượngGiá vật phẩmGiá bánStorageCity
1 1 1 10 10 Một
1 2 2 20 40 B
1 3 1 30 30 C
1 4 1 40 40 Một
1 5 2 50 100 B
2 1 3 10 30 C
2 2 1 20 20 Một
2 3 2 30 60 B
3 1 2 10 20 C
3 2 1 20 20 Một
3 3 3 30 90 B
3 1 1 10 10 C

Đối với mỗi customer_id, tôi muốn giao một trang với các bảng như (ví dụ: id_ = 1 của khách hàng

  1. Hàng hóa nào đã được mua và tổng giá của chúng là bao nhiêu
item_idsố lượngGiá vật phẩmGiá bán
1 1 10 10
2 2 20 40
3 1 30 30
4 1 40 40
5 2 50 100
StorageCityStorageCityStorageCityStorageCity
Một 7 220

B

StorageCityMột
Một 2
B 4
C 1
StorageCityStorageCity
Một 7

B

import pandas as pd 
import numpy as np
from fpdf import FPDF



class PDF(FPDF):
    def header(self):
        # Logo
        #self.image('logo.png', 10, 8, 33)
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, 'Bill', 1, 0, 'C')
        # Line break
        self.ln(20)

    # Page footer
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
        
    def doc_header(self, num, label):
        # Arial 12
        self.set_font('Arial', '', 12)
        # Background color
        self.set_fill_color(200, 220, 255)
        # Title
        self.cell(0, 6, 'Customer number: %d - %s -' % (num, label), 0, 1, 'L', 1)
        # Line break
        self.ln(4)
        
        
    def print_doc(self, num, title):
        self.add_page()
        self.doc_header(num, title)


### Read csv
ipl_data = pd.read_csv("test.csv", delimiter=";", header=0)
df = pd.DataFrame(ipl_data)
pdf = PDF()

df3 = df.groupby(['customer_id'])
df5 = df.groupby(['customer_id','StorageCity'])



for key,group_df in df3:
    pdf.alias_nb_pages()
    pdf.set_font('Times', '', 12)
    
    page_width = pdf.w - 2 * pdf.l_margin
    col_width = page_width/4
    pdf.print_doc(key, 'test')
    #print(group_df)
    #print("the group for product '{}' has {} rows".format(key,len(group_df)))
   
    pdf.ln(1)
    th = pdf.font_size
    pdf.cell(col_width, th, df.columns[1], border=1)
    pdf.cell(col_width, th, df.columns[2], border=1)
    pdf.cell(col_width, th, df.columns[3], border=1)
    pdf.cell(col_width, th, df.columns[4], border=1) 
    pdf.ln(th)


    for row_index, row in group_df.iterrows():
        pdf.cell(col_width, th, format(row['item_id']), border=1)
        pdf.cell(col_width, th, format(row['amount']), border=1)
        pdf.cell(col_width, th, format(row['ItemPrice']), border=1)
        pdf.cell(col_width, th, format(row['Price']), border=1)
        pdf.ln(th)
    pdf.ln(1)
    pdf.ln(1)


for key,group_df in df5:

    pdf.ln(1)
    th = pdf.font_size
    pdf.cell(col_width, th, df.columns[5], border=1)
    pdf.cell(col_width, th, df.columns[1], border=1)

    pdf.ln(th)


    for row_index, row in group_df.iterrows():
        pdf.cell(col_width, th, format(row['StorageCity']), border=1)
        pdf.cell(col_width, th, format(row['item_id']), border=1)


        pdf.ln(th)
    pdf.ln(1)
    pdf.ln(1)
    


pdf.output('test.pdf', 'F')

C

Đối với mỗi customer_id, tôi muốn giao một trang với các bảng như (ví dụ: id_ = 1 của khách hàng

Hàng hóa nào đã được mua và tổng giá của chúng là bao nhiêua 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.

StorageCity

B. tabula-py also enables you to convert a PDF file into CSV/TSV/JSON file.

Một

C.

FPDF trong Python là gì?

Django. FPDF là gì? PYFPDF là một thư viện trong Python, nó được sử dụng để tạo tài liệu tệp PDF. 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à nhiều hơn nữa.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.

Gandas có thể đọc PDF không?

Bạn có thể đọc các bảng từ PDF và chuyển đổi thành DataFrame của Pandas.Tabula-Py cũng cho phép bạn chuyển đổi tệp PDF thành tệp CSV/TSV/JSON.. tabula-py also enables you to convert a PDF file into CSV/TSV/JSON file.

Làm thế nào để bạn tạo một bảng từ PDF trong Python?.

Tạo một bảng trong PDF bằng Python ...

Đầu tiên, chúng tôi phải cài đặt PDFKIT bằng cách sử dụng Pip Install PDFKit ..