forked from ScoDoc/ScoDoc
222 lines
6.9 KiB
Python
222 lines
6.9 KiB
Python
# -*- mode: python -*-
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
#
|
|
# Gestion scolarite IUT
|
|
#
|
|
# Copyright (c) 1999 - 2019 Emmanuel Viennet. All rights reserved.
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
#
|
|
# Emmanuel Viennet emmanuel.viennet@viennet.net
|
|
#
|
|
##############################################################################
|
|
|
|
|
|
""" Excel file handling
|
|
"""
|
|
|
|
# from sco_utils import *
|
|
# from sco_excel import *
|
|
import time, datetime
|
|
|
|
from openpyxl.styles import Font, Alignment, PatternFill, Side, Border
|
|
|
|
from app.scodoc.sco_excel import COLORS, ScoExcelSheet
|
|
|
|
|
|
def make_font(
|
|
bold=False,
|
|
italic=False,
|
|
font_name=None,
|
|
font_size=None,
|
|
color=COLORS.BLACK.value,
|
|
style=None,
|
|
uline=False,
|
|
):
|
|
font = None
|
|
if bold or italic or font_name or font_size:
|
|
font = Font()
|
|
if bold:
|
|
font.bold = bold
|
|
if uline:
|
|
font.underline = Font.UNDERLINE_SINGLE
|
|
if italic:
|
|
font.italic = italic
|
|
font.name = font_name if font_name else "Arial"
|
|
if font_size:
|
|
font.height = 20 * font_size
|
|
if color:
|
|
font.color = color
|
|
if font and style:
|
|
style["font"] = font
|
|
return font
|
|
|
|
|
|
def make_alignment(halign=None, valign=None, orientation=None, style=None):
|
|
alignment = None
|
|
if halign or valign or orientation:
|
|
alignment = Alignment()
|
|
if halign:
|
|
alignment.horz = halign
|
|
if valign:
|
|
alignment.vert = valign
|
|
if orientation:
|
|
alignment.rota = orientation
|
|
if alignment and style:
|
|
breakpoint()
|
|
style["alignment"] = alignment
|
|
return alignment
|
|
|
|
|
|
def make_numfmt(fmt, style=None):
|
|
if fmt and style:
|
|
style["num_format_str"] = fmt
|
|
|
|
|
|
def make_pattern(bgcolor, style=None):
|
|
pattern = PatternFill(fill_type="solid", fgColor=bgcolor)
|
|
if pattern and style:
|
|
style["fill"] = pattern
|
|
return pattern
|
|
|
|
|
|
Sides = {
|
|
"none": Side(border_style=None),
|
|
"thin": Side(border_style="thin"),
|
|
"medium": Side(border_style="medium"),
|
|
"thick": Side(border_style="thick"),
|
|
"filet": Side(border_style="hair"),
|
|
}
|
|
|
|
|
|
def make_borders(left=None, top=None, bottom=None, right=None, style=None):
|
|
border = None
|
|
if left or right or top or bottom:
|
|
border = Border()
|
|
if left:
|
|
border.left = Sides[left]
|
|
if right:
|
|
border.right = Sides[right]
|
|
if top:
|
|
border.top = Sides[top]
|
|
if bottom:
|
|
border.bottom = Sides[bottom]
|
|
if border and style:
|
|
style["border"] = border
|
|
return border
|
|
|
|
|
|
#
|
|
#
|
|
# def Excel_MakeStyle(
|
|
# bold=False,
|
|
# italic=False,
|
|
# color="black",
|
|
# bgcolor=None,
|
|
# halign=None,
|
|
# valign=None,
|
|
# orientation=None,
|
|
# font_name=None,
|
|
# font_size=None,
|
|
# ):
|
|
# style = XFStyle()
|
|
# make_font(bold, italic, font_name, font_size, color, style=style)
|
|
# make_alignment(halign, valign, orientation, style=style)
|
|
# make_pattern(bgcolor, style=style)
|
|
# return style
|
|
#
|
|
#
|
|
# class ScoExcelSheetExt(ScoExcelSheet):
|
|
# def __init__(self, sheet_name="feuille", default_style=None):
|
|
# ScoExcelSheet.__init__(self, sheet_name, default_style)
|
|
# self.cols_width = {}
|
|
# self.row_height = {}
|
|
# self.merged = []
|
|
# self.panes_frozen = False
|
|
# self.horz_split_pos = 0
|
|
# self.vert_split_pos = 0
|
|
#
|
|
# def set_panes(self, panes_frozen=True, horz_split_pos=0, vert_split_pos=0):
|
|
# self.panes_frozen = panes_frozen
|
|
# self.horz_split_pos = horz_split_pos
|
|
# self.vert_split_pos = vert_split_pos
|
|
#
|
|
# def set_cols_width(self, cols_width):
|
|
# self.cols_width = cols_width
|
|
#
|
|
# def set_row_height(self, row_height):
|
|
# self.row_height = row_height
|
|
#
|
|
# def add_merged(self, first_row, last_row, first_col, last_col, value, style):
|
|
# self.merged.append((first_row, last_row, first_col, last_col, value, style))
|
|
#
|
|
# def gen_workbook(self, wb=None):
|
|
# """Generates and returns a workbook from stored data.
|
|
# If wb, add a sheet (tab) to the existing workbook (in this case, returns None).
|
|
# """
|
|
# if wb == None:
|
|
# wb = Workbook() # Création du fichier
|
|
# sauvegarde = True
|
|
# else:
|
|
# sauvegarde = False
|
|
# ws0 = wb.add_sheet(self.sheet_name.decode(SCO_ENCODING))
|
|
# li = 0
|
|
# for l in self.cells:
|
|
# co = 0
|
|
# for c in l:
|
|
# # safety net: allow only str, int and float
|
|
# if type(c) == LongType:
|
|
# c = int(c) # assume all ScoDoc longs fits in int !
|
|
# elif type(c) not in (IntType, FloatType):
|
|
# c = str(c).decode(SCO_ENCODING)
|
|
#
|
|
# ws0.write(li, co, c, self.get_cell_style(li, co))
|
|
# co += 1
|
|
# li += 1
|
|
# for first_row, last_row, first_col, last_col, value, style in self.merged:
|
|
# ws0.write_merge(
|
|
# first_row,
|
|
# last_row,
|
|
# first_col,
|
|
# last_col,
|
|
# str(value).decode(SCO_ENCODING),
|
|
# style,
|
|
# )
|
|
# for col_no in range(len(self.cols_width)):
|
|
# width = self.cols_width[col_no]
|
|
# ws0.col(col_no).width = abs(width) * 110 / 3
|
|
# if width < 0:
|
|
# ws0.col(col_no).hidden = True
|
|
# row_styles = {}
|
|
# for row_no in range(len(self.row_height)):
|
|
# height = self.row_height[row_no]
|
|
# if height not in row_styles:
|
|
# fnt = Font()
|
|
# fnt.height = height * 12
|
|
# row_styles[height] = XFStyle()
|
|
# row_styles[height].font = fnt
|
|
# ws0.row(row_no).set_style(row_styles[height]) # Excel way
|
|
# ws0.row(row_no).height = height * 19 / 2 # Libre-Office compatibilité
|
|
# if self.panes_frozen:
|
|
# ws0.panes_frozen = self.panes_frozen
|
|
# ws0.horz_split_pos = self.vert_split_pos
|
|
# ws0.vert_split_pos = self.horz_split_pos
|
|
# if sauvegarde:
|
|
# return wb.savetostr()
|
|
# else:
|
|
# return None
|