代码之家  ›  专栏  ›  技术社区  ›  ozo

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

  •  4
  • ozo  · 技术社区  · 7 年前

    我有一个评估大学教授的调查数据库。我想要的是一个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。

    2 回复  |  直到 7 年前
        1
  •  19
  •   patrickjlong1    7 年前

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

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

    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')
    

    Expected test.pdf

        2
  •  0
  •   Fernando Garcia    7 年前

    就我而言:

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