【发布时间】:2021-03-16 19:04:15
【问题描述】:
我是 python 新手,但已经遇到了问题。
基于 .csv 表我想创建一个 pdf 文件
| customer_id | item_id | amount | ItemPrice | Price | StorageCity |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 10 | 10 | A |
| 1 | 2 | 2 | 20 | 40 | B |
| 1 | 3 | 1 | 30 | 30 | C |
| 1 | 4 | 1 | 40 | 40 | A |
| 1 | 5 | 2 | 50 | 100 | B |
| 2 | 1 | 3 | 10 | 30 | C |
| 2 | 2 | 1 | 20 | 20 | A |
| 2 | 3 | 2 | 30 | 60 | B |
| 3 | 1 | 2 | 10 | 20 | C |
| 3 | 2 | 1 | 20 | 20 | A |
| 3 | 3 | 3 | 30 | 90 | B |
| 3 | 1 | 1 | 10 | 10 | C |
对于每个 customer_id,我希望提供一个页面,其中包含如下表格(例如 customer id_=1):
- 购买了哪些商品,总价是多少
| item_id | amount | ItemPrice | Price |
|---|---|---|---|
| 1 | 1 | 10 | 10 |
| 2 | 2 | 20 | 40 |
| 3 | 1 | 30 | 30 |
| 4 | 1 | 40 | 40 |
| 5 | 2 | 50 | 100 |
| -------- | -------- | -------- | -------- |
| Sum | 7 | 220 |
和 2. 多少产品来自哪个仓库
| StorageCity | SumAmount |
|---|---|
| A | 2 |
| B | 4 |
| C | 1 |
| -------- | -------- |
| Sum | 7 |
我尝试了以下解决方案,但我无法正确访问数据
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')
如有任何提示,我将不胜感激。
【问题讨论】:
标签: python pandas dataframe fpdf