Hướng dẫn python fpdf table from dataframe - bảng fpdf python từ dataframe

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

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ột7 220

B

StorageCityMột
Một2
B4
C1
StorageCityStorageCity
Một7

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

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 ..
Chúng tôi phải tải xuống wkhtmltopdf bằng cách sử dụng liên kết: wkhtmltopdf ..
Sau khi tải xuống, chúng tôi phải sao chép đường dẫn của tệp wkhtmltopdf và dán nó vào biến môi trường ..

Làm cách nào để chuyển đổi tệp PDF thành DataFrame trong Python?

Lưu ý: Tất cả các dòng bắt đầu bằng # đều là nhận xét ...
Bước 1: Nhập tất cả các thư viện.Nhập Tabula#PD là tốc ký tiêu chuẩn cho gấu trúc.Nhập Gandas dưới dạng PD ..
Bước 2: Chuyển đổi bảng PDF của bạn thành DataFrame.#Declare đường dẫn của tập tin của bạn.file_path = "/path/to/pdf_file/data.pdf"#convert tệp của bạn ..