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

eyh26e7m  于 2023-04-10  发布在  其他
关注(0)|答案(4)|浏览(131)

我有一个数据库,是由一个评估大学教授的调查生成的。我想要的是一个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报告,通过图表汇总此信息,例如每个老师的整体得分的图表,每个老师按类别的得分的另一个图表,每个学生的平均值的另一个图表等。最后,每个老师都会有一个报告。我想要一个像this

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

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

nbysray51#

在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'].iloc[i]), 1, 0, 'C')
    pdf.cell(40, 10, '%s' % (str(df.Mike.iloc[i])), 1, 0, 'C')
    pdf.cell(40, 10, '%s' % (str(df.Charles.iloc[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:

**更新(2020年4月):**我在2020年4月对原始答案进行了编辑,以替换pandas.DataFrame.ix()的使用,因为这是deprecated。在我的示例中,我能够将其使用替换为pandas.DataFrame.iloc,输出与以前相同。

ttcibm8c

ttcibm8c2#

一个有点异端的答案:RMarkdown(在RStudio中),使用Python代码块,通过reticulate(现在的默认方式),就像在Jypiter笔记本中一样,为您提供长期的Python“会话”。RMarkdown文档可以“编织”成PDF,html,Word,html幻灯片,甚至PowerPoint。
说真的,R世界在这个领域领先几条街。

ewm0tg9j

ewm0tg9j3#

我同意@drz关于RMarkdown创建这样一个报告的观点。学术作品应该明确使用这个。无论如何,还有stitch,它确实使用起来很简单,在很多情况下可能就足够了。来自FPF的许多优点:

  • 管理分页
  • 标记语法可用
  • matplotlib和pandas图直接输出
  • 可以生成html或pdf

以下是stitch中的@patrickjlong1示例:

# Stich is simple and great

## Usefull markup language

You can use markdown syntax, such as **bold**, _italic_, ~~Strikethrough~~

## display dataframes

Direct output from python will be nicelly output.

```{python, echo=False}

import pandas as pd

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

display graphics

Direct matplotlib output, without rendering to file.

#%matplotlib inline
df.plot.bar(title="Professor Criss's Ratings by Users")
None

Symbolic expressions

You may also want to work with sympy :


import sympy
sympy.init_printing()
x=sympy.symbol.Symbol('x')
sympy.integrate(sympy.sqrt(1/sympy.sin(x**2)))

安装后,将使用以下内容创建PDF:

stitch test2.stich -o output.pdf


输出如下所示:

![](https://i.stack.imgur.com/apygY.png)
1l5u6lss

1l5u6lss4#

在我的例子中:

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

希望这能帮上忙。

相关问题