Esempio n. 1
0
    def cmd_export(self):
        bkg_color = self.app.colors["color_3"].get().replace("#", "")
        bkg_fill = PatternFill(patternType='solid', fgColor=Color(rgb=bkg_color))

        head_color = self.app.colors["color_1"].get().replace("#", "")
        head_fill = PatternFill(patternType='solid', fgColor=Color(rgb=head_color))

        head_font_color = self.app.colors["font_color_1"].get().replace("#", "")
        head_font = Font(bold=True, color=head_font_color)

        body_font_color = self.app.colors["font_color_2"].get().replace("#", "")
        body_font = Font(bold=False, color=body_font_color)

        align_center = Alignment(horizontal="center", vertical="center")
        align_right = Alignment(horizontal="right", vertical="center")
        align_left = Alignment(horizontal="left", vertical="center")

        row_offset = 1
        col_offset = 1
        work_book = Workbook()
        work_sheet = work_book.active
        work_sheet.title = "CS4_output"

        for xl_row in range(1, 100):
            for xl_col in range(1, 100):
                active_cell = work_sheet.cell(column=xl_col, row=xl_row)
                active_cell.fill = bkg_fill

        for xl_row, res_row in enumerate(self.result_table):
            for xl_col, res_col in enumerate(res_row):
                active_cell = work_sheet.cell(column=xl_col+col_offset,
                                              row=xl_row+row_offset,
                                              value="{0}".format(res_col))
                # Left column header
                if not xl_col:
                    active_cell.fill = head_fill
                    active_cell.font = head_font
                    active_cell.alignment = align_right
                # Top row header
                elif not xl_row:
                    active_cell.fill = head_fill
                    active_cell.font = head_font
                    active_cell.alignment = align_center
                # Data row at the bottom
                elif xl_row >= len(self.result_table)-3:
                    active_cell.value = int(res_col)
                # Style names
                else:
                    active_cell.font = body_font
                    active_cell.alignment = align_left
        
        # Create chart
        values = Reference(work_sheet,
                           min_col=col_offset+1,
                           max_col=len(self.result_table[0])+col_offset-1,
                           min_row=len(self.result_table)+row_offset-1,
                           max_row=len(self.result_table)+row_offset-1)
        chart = AreaChart()
        chart.add_data(values, from_rows=True)
        chart.legend = None
        chart.title = "Core Sound analysis"

        # set a pattern for the whole series
        series = chart.series[0]
        fill = PatternFillProperties()
        fill.foreground = ColorChoice(prstClr="red")
        fill.background = ColorChoice(prstClr="blue")
        series.graphicalProperties.pattFill = fill

        work_sheet.add_chart(chart, "E15")

        work_book.save(filename=self.file_name)
        self.root.destroy()
wb = Workbook()
ws = wb.active

rows = [
    ['Number', 'Batch 1', 'Batch 2'],
    [2, 30, 40],
    [3, 25, 40],
    [4, 30, 50],
    [5, 10, 30],
    [6, 5, 25],
    [7, 10, 50],
]

for row in rows:
    ws.append(row)

chart = AreaChart3D()
chart.title = "Area Chart"
chart.style = 13
chart.x_axis.title = 'Test'
chart.y_axis.title = 'Percentage'
chart.legend = None

cats = Reference(ws, min_col=1, min_row=1, max_row=7)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)

ws.add_chart(chart, "A10")

wb.save("area3D.xlsx")