Python-从数据库创建带有图表的pdf报告的过程是什么?

2024-04-18 20:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个评估大学教授的调查数据库。我想要的是一个python脚本,它从数据库中获取信息,为每个用户生成一个图形表,为每个用户创建图形,然后将其呈现在模板中以将其导出为pdf。

数据库是什么样子的?

User    Professor_evaluated  Category       Question    Answer
_________________________________________________________________
Mike    Professor Criss       respect           1         3
Mike    Professor Criss       respect           2         4
Mike    Professor Criss       wisdom            3         5
Mike    Professor Criss       wisdom            4         3
Charles Professor Criss       respect           1         3
Charles Professor Criss       respect           2         4
Charles Professor Criss       wisdom            3         5
Charles Professor Criss       wisdom            4         3

每一位老师都有几个类别要被评估(尊重、智慧等),而每个类别又有相关的问题。换句话说,一个类别有几个问题。数据库的每一行都是对学生评价老师的问题的回答

我需要什么?

我需要创建一个脚本来自动生成pdf报告,通过图表来总结这些信息,例如,一个图表包含每个教师的总分,另一个图表包含每个教师按类别的得分,另一个图表包含每个学生的平均分,等等。最后,每个老师都会有一份报告。我想要这样的报告Example

我的问题是什么?

我的问题是我需要哪些python包和模块来完成这项任务。这样做的一般过程是什么。我不需要代码,因为我知道答案很笼统,但我知道如何做到这一点。

例如:您首先需要使用pandas处理信息,创建一个汇总要绘制的信息的表,然后绘制它,然后使用XYZ模块创建报表模板,然后使用XYZ模块将其导出到pdf。


Tags: 模块脚本信息数据库pdf报告图表老师
2条回答

在python中创建pdf有很多选项。其中一些选项是ReportLab、pydf2、pdfdocument和FPDF。

FPDF库的使用非常简单,这就是我在本例中使用的。可以找到FPDF文档here

考虑一下您可能希望使用哪些python模块来创建图和表,这可能也是一个好主意。在我的示例中,我使用matplotlib(link to docs),还使用Pandas创建了一个使用pandas.dataframe()的数据帧。

我在下面发布了一个相当长但完全可复制的示例,使用pandas、matplotlib和fpdf。数据是问题中OP提供的内容的子集。我在示例中循环遍历dataframe来创建表,但是有其他的方法,也许更有效。

import pandas as pd
import matplotlib
from pylab import title, figure, xlabel, ylabel, xticks, bar, legend, axis, savefig
from fpdf import FPDF


df = pd.DataFrame()
df['Question'] = ["Q1", "Q2", "Q3", "Q4"]
df['Charles'] = [3, 4, 5, 3]
df['Mike'] = [3, 3, 4, 4]

title("Professor Criss's Ratings by Users")
xlabel('Question Number')
ylabel('Score')

c = [2.0, 4.0, 6.0, 8.0]
m = [x - 0.5 for x in c]

xticks(c, df['Question'])

bar(m, df['Mike'], width=0.5, color="#91eb87", label="Mike")
bar(c, df['Charles'], width=0.5, color="#eb879c", label="Charles")

legend()
axis([0, 10, 0, 8])
savefig('barchart.png')

pdf = FPDF()
pdf.add_page()
pdf.set_xy(0, 0)
pdf.set_font('arial', 'B', 12)
pdf.cell(60)
pdf.cell(75, 10, "A Tabular and Graphical Report of Professor Criss's Ratings by Users Charles and Mike", 0, 2, 'C')
pdf.cell(90, 10, " ", 0, 2, 'C')
pdf.cell(-40)
pdf.cell(50, 10, 'Question', 1, 0, 'C')
pdf.cell(40, 10, 'Charles', 1, 0, 'C')
pdf.cell(40, 10, 'Mike', 1, 2, 'C')
pdf.cell(-90)
pdf.set_font('arial', '', 12)
for i in range(0, len(df)):
    pdf.cell(50, 10, '%s' % (df['Question'].ix[i]), 1, 0, 'C')
    pdf.cell(40, 10, '%s' % (str(df.Mike.ix[i])), 1, 0, 'C')
    pdf.cell(40, 10, '%s' % (str(df.Charles.ix[i])), 1, 2, 'C')
    pdf.cell(-90)
pdf.cell(90, 10, " ", 0, 2, 'C')
pdf.cell(-30)
pdf.image('barchart.png', x = None, y = None, w = 0, h = 0, type = '', link = '')
pdf.output('test.pdf', 'F')

预期测试.pdf:

Expected test.pdf

就我而言:

  • 使用cx_Oracle库连接到Oracle数据库并提取数据
  • 使用Pandas数据帧进行数据操作
  • 使用Matplotlib生成图形
  • 使用ExcelWriter和ReportLab以Excel或PDF格式输出

希望这有帮助。

相关问题 更多 >