Exemplo n.º 1
0
def draw():
    report = Report("A calendar-like report")     # Create report object
    rs = report.rows
    cs = report.cols

    report.cols_width = 1600

    # Create row headers (calendar)
    for week in weekgen(datetime.now() - timedelta(days=30), datetime.now()):
        wes = rs.add_section()
        for day in week:
            if day.weekday() in (5,6):
                style = cell_style_weekend
            else:
                style = cell_style
            wes.add_field(day.strftime("%b %d"), style=style)
        wes.add_calc('Week %s' % weeknum(week[0]), func=sum, style=row_header_style)
    rs.add_calc('Total', func=sum, style=total_style)


    # Create column headers (just one calc field that computes average)
    cs.add_calc('Average', func=lambda x: 1.0*sum(x)/len(x), style=total_style)

    # Generate random data
    data = [[randrange(100) for i in range(5)] for j in range(30)]

    book = Workbook()
    ws = book.add_sheet('Worksheet')
    report.render(ws, data)             # Render report
    book.save(report_name)              # Save report
Exemplo n.º 2
0
Arquivo: test.py Projeto: p0123n/xlrep
    def test_report_general(self):
        # Prepare
        r = Report()
        r.caption = 'test_report_0'

        hs = r.cols
        hs.add_field('col 0')
        hs.add_field('col 1')
        hss = hs.add_section('col section 0')
        hss.add_field('col 2_1')
        hss.add_calc('col total', sum)
        hs.add_field('col 3')
        hs.add_calc('col total 0', func=lambda x: 1.0*sum(x)/len(x))

        rs = r.rows
        rs.add_field('row 0')
        rss = rs.add_section('row section 0')
        rss.add_field('row 1_1')
        rss.add_field('row 1_2')
        rss.add_calc('row total 0', func=sum)
        rs.add_field('row 2')
        rs.add_field('row 3')
        rs.add_calc('row total 1', func=sum)

        book = Workbook()
        ws = book.add_sheet('test worksheet')
        data = (range(4) for i in range(5))
        r.render(ws, data)
        compiled_report = StringIO()
        book.save(compiled_report)

        # Test

        book = xlrd.open_workbook(file_contents=compiled_report.getvalue())
        ws = book.sheet_by_index(0)

        test_data = [
            [0.0, 1.0, 2.0, 2.0, 3.0, 1.5],
            [0.0, 1.0, 2.0, 2.0, 3.0, 1.5],
            [0.0, 1.0, 2.0, 2.0, 3.0, 1.5],
            [0.0, 2.0, 4.0, 4.0, 6.0, 3.0],
            [0.0, 1.0, 2.0, 2.0, 3.0, 1.5],
            [0.0, 1.0, 2.0, 2.0, 3.0, 1.5],
            [0.0, 5.0, 10.0, 10.0, 15.0, 7.5]
        ]

        for i in range(3,10):
            for j in range(2,8):
                self.assertEquals(ws.cell(i,j).value, test_data[i-3][j-2])
Exemplo n.º 3
0
Arquivo: test.py Projeto: p0123n/xlrep
    def test_report_calc_2(self):
        """Test fields feature"""

        # Prepare
        r = Report()

        hs = r.cols
        c0 = hs.add_field('col 0')
        c1 = hs.add_field('col 1')
        hs.add_field('col ignore')
        hs.add_calc('col total', sum, fields=(c0,c1))

        rs = r.rows
        r0 = rs.add_field('row 0')
        r1 = rs.add_field('row 1')
        rs.add_field('row ignore')
        rs.add_calc('row total', sum, fields=(r0,r1))

        data = ((1,1,1),(2,2,2),(3,3,3))
        book = Workbook()
        ws = book.add_sheet('test worksheet')

        r.render(ws, data)
        compiled_report = StringIO()
        book.save(compiled_report)
        #book.save(test_file)

        # Test

        book = xlrd.open_workbook(file_contents=compiled_report.getvalue())
        ws = book.sheet_by_index(0)

        test_data = [
            [1.0, 1.0, 1.0, 2.0],
            [2.0, 2.0, 2.0, 4.0],
            [3.0, 3.0, 3.0, 6.0],
            [3.0, 3.0, 3.0, 6.0],
        ]

        #system("oowriter %s" % test_file)

        for i in range(4):
            for j in range(4):
                self.assertEquals(ws.cell(i+1,j+1).value, test_data[i][j])
Exemplo n.º 4
0
Arquivo: test.py Projeto: p0123n/xlrep
    def test_report_calc_1(self):
        """Test cross_fields_ignore feature"""

        # Prepare
        r = Report()
        r.caption = 'test_report_calc_1'

        hs = r.cols
        hs.add_field('col 0')
        hs.add_field('col 1')
        col2 = hs.add_field('col ignore')
        hs.add_calc('col total', sum)

        rs = r.rows
        rs.add_field('row 0')
        rs.add_field('row 1')
        rs.add_field('row 2')
        rs.add_calc('row total', sum, cross_fields_ignore=(col2,))

        data = ((1,1,1),(2,2,2),(3,3,3))
        book = Workbook()
        ws = book.add_sheet('test worksheet')

        r.render(ws, data)
        compiled_report = StringIO()
        book.save(compiled_report)
        book.save(test_file)

        # Test

        book = xlrd.open_workbook(file_contents=compiled_report.getvalue())
        ws = book.sheet_by_index(0)

        test_data = [
            [1.0, 1.0, 1.0, 3.0],
            [2.0, 2.0, 2.0, 6.0],
            [3.0, 3.0, 3.0, 9.0],
            [6.0, 6.0, '', 12.0],
        ]

        for i in range(4):
            for j in range(4):
                self.assertEquals(ws.cell(i+2,j+1).value, test_data[i][j])
Exemplo n.º 5
0
from xlrep import Report
from xlwt import Workbook, easyxf
from random import randrange

calc_style = easyxf("pattern: pattern solid, fore-colour gray25;")
header_style = easyxf("pattern: pattern solid, fore-colour rose;")
total_style = easyxf("font: bold on;")

report_name = "quadric.xls"
rep = Report("Quadric report")  # Create report object

cs = rep.cols  # Create alias for column and row sections
rs = rep.rows

for i in range(3):  # Create row sections
    ccs = cs.add_section("col %d" % i)
    for j in range(3):
        ccs.add_field("%d" % j, header_style=header_style)
    ccs.add_calc("", func=sum, style=calc_style)
cs.add_calc("grand total", func=sum, style=total_style)

for i in range(3):  # Create column sections
    rrs = rs.add_section("row %d" % i)
    for j in range(3):
        rrs.add_field("%d" % j, header_style=header_style)
    rrs.add_calc("", func=sum, style=calc_style)
rs.add_calc("grand total", func=sum, style=total_style)

rep.cols_width = 1200

data = [[randrange(10) for j in range(9)] for i in range(9)]  # Generate data
Exemplo n.º 6
0
from xlrep import Report
from xlwt import Workbook

report_name = 'simple.xls'

rep = Report("A simple report")     # Create report object

cs = rep.cols                       # Add columns
cs.add_field('col 0')
cs.add_field('col 1')
css = cs.add_section('section 0')   # Add a subsection
css.add_field('col 3')              # Add fields to the subsection
css.add_field('col 4')

rs = rep.rows                       # Add rows
rs.add_calc('total', func=sum)      # Add just a calc field

data = (range(4) for i in range(10))    # Generate some data
                                        # Note: dimentions of data matrix should
                                        # be compatible with number of data
                                        # fields 

book = Workbook()                   # xlwt part: create Workbook and Worksheet
ws = book.add_sheet('Worksheet')
rep.render(ws, data)                # Render report
book.save(report_name)              # Save report

print 'Report has been constructed:', report_name