Max number of chars per column configurable in XLSX.
Also added row height adjust.
This commit is contained in:
parent
7298a5662f
commit
77bf713b26
|
|
@ -5,8 +5,8 @@ This code is adapted from https://github.com/SchrodingersGat/KiBoM by Oliver Hen
|
||||||
|
|
||||||
Generates an XLSX file.
|
Generates an XLSX file.
|
||||||
"""
|
"""
|
||||||
# TODO: alternate colors, set row height, put logo, move data to beginning
|
|
||||||
import io
|
import io
|
||||||
|
from textwrap import wrap
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
from .columnlist import ColumnList
|
from .columnlist import ColumnList
|
||||||
from .kibot_logo import KIBOT_LOGO
|
from .kibot_logo import KIBOT_LOGO
|
||||||
|
|
@ -21,7 +21,6 @@ except ModuleNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
logger = log.get_logger(__name__)
|
logger = log.get_logger(__name__)
|
||||||
MAX_WIDTH = 60
|
|
||||||
BG_GEN = "#E6FFEE"
|
BG_GEN = "#E6FFEE"
|
||||||
BG_KICAD = "#FFE6B3"
|
BG_KICAD = "#FFE6B3"
|
||||||
BG_USER = "#E6F9FF"
|
BG_USER = "#E6F9FF"
|
||||||
|
|
@ -207,6 +206,9 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg):
|
||||||
head_size = compute_head_size(cfg)
|
head_size = compute_head_size(cfg)
|
||||||
# First rowe for the information
|
# First rowe for the information
|
||||||
r_info_start = 1 if cfg.xlsx.title else 0
|
r_info_start = 1 if cfg.xlsx.title else 0
|
||||||
|
max_width = cfg.xlsx.max_col_width
|
||||||
|
if max_width < 20:
|
||||||
|
max_width = 20
|
||||||
|
|
||||||
# #######################
|
# #######################
|
||||||
# Create all the formats
|
# Create all the formats
|
||||||
|
|
@ -242,6 +244,7 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg):
|
||||||
# Headings
|
# Headings
|
||||||
# Create the head titles
|
# Create the head titles
|
||||||
column_widths = [0]*len(col_fields)
|
column_widths = [0]*len(col_fields)
|
||||||
|
rows = [row_headings]
|
||||||
for i in range(len(row_headings)):
|
for i in range(len(row_headings)):
|
||||||
# Title for this column
|
# Title for this column
|
||||||
column_widths[i] = len(row_headings[i]) + 10
|
column_widths[i] = len(row_headings[i]) + 10
|
||||||
|
|
@ -254,6 +257,7 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg):
|
||||||
continue
|
continue
|
||||||
# Get the data row
|
# Get the data row
|
||||||
row = group.get_row(col_fields)
|
row = group.get_row(col_fields)
|
||||||
|
rows.append(row)
|
||||||
if link_datasheet != -1:
|
if link_datasheet != -1:
|
||||||
datasheet = group.get_field(ColumnList.COL_DATASHEET_L)
|
datasheet = group.get_field(ColumnList.COL_DATASHEET_L)
|
||||||
# Fill the row
|
# Fill the row
|
||||||
|
|
@ -286,7 +290,6 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg):
|
||||||
# PCB & Stats Info
|
# PCB & Stats Info
|
||||||
if not (cfg.xlsx.hide_pcb_info and cfg.xlsx.hide_stats_info):
|
if not (cfg.xlsx.hide_pcb_info and cfg.xlsx.hide_stats_info):
|
||||||
rc = r_info_start
|
rc = r_info_start
|
||||||
# TODO: Language?
|
|
||||||
if not cfg.xlsx.hide_pcb_info:
|
if not cfg.xlsx.hide_pcb_info:
|
||||||
rc = add_info(worksheet, column_widths, rc, col1, fmt_info, "Schematic:", cfg.source)
|
rc = add_info(worksheet, column_widths, rc, col1, fmt_info, "Schematic:", cfg.source)
|
||||||
rc = add_info(worksheet, column_widths, rc, col1, fmt_info, "Variant:", ' + '.join(cfg.variant))
|
rc = add_info(worksheet, column_widths, rc, col1, fmt_info, "Variant:", ' + '.join(cfg.variant))
|
||||||
|
|
@ -305,10 +308,20 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg):
|
||||||
# Adjust the widths
|
# Adjust the widths
|
||||||
for i in range(len(column_widths)):
|
for i in range(len(column_widths)):
|
||||||
width = column_widths[i]
|
width = column_widths[i]
|
||||||
if width > MAX_WIDTH:
|
if width > max_width:
|
||||||
width = MAX_WIDTH
|
width = max_width
|
||||||
worksheet.set_column(i, i, width)
|
worksheet.set_column(i, i, width)
|
||||||
|
|
||||||
|
# Adjust the heights
|
||||||
|
for rn, r in enumerate(rows):
|
||||||
|
max_h = 1
|
||||||
|
for c in r:
|
||||||
|
if len(c) > max_width:
|
||||||
|
h = len(wrap(c, max_width))
|
||||||
|
max_h = max(h, max_h)
|
||||||
|
if max_h > 1:
|
||||||
|
worksheet.set_row(head_size+rn, 15.0*max_h)
|
||||||
|
|
||||||
worksheet.freeze_panes(head_size+1, 0)
|
worksheet.freeze_panes(head_size+1, 0)
|
||||||
worksheet.repeat_rows(head_size+1)
|
worksheet.repeat_rows(head_size+1)
|
||||||
worksheet.set_landscape()
|
worksheet.set_landscape()
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,8 @@ class BoMRegex(Optionable):
|
||||||
self.regexp = None
|
self.regexp = None
|
||||||
""" {regex} """ # pragma: no cover
|
""" {regex} """ # pragma: no cover
|
||||||
|
|
||||||
def __str__(self):
|
# def __str__(self):
|
||||||
# TODO make a list
|
# return self.column+'\t'+self.regex
|
||||||
return self.column+'\t'+self.regex
|
|
||||||
|
|
||||||
|
|
||||||
class BoMColumns(Optionable):
|
class BoMColumns(Optionable):
|
||||||
|
|
@ -146,6 +145,8 @@ class BoMXLSX(Optionable):
|
||||||
""" Hide statistics information """
|
""" Hide statistics information """
|
||||||
self.logo = Optionable
|
self.logo = Optionable
|
||||||
""" [string|boolean] PNG file to use as logo, use false to remove """
|
""" [string|boolean] PNG file to use as logo, use false to remove """
|
||||||
|
self.max_col_width = 60
|
||||||
|
""" [20,999] Maximum column width (characters) """
|
||||||
self.style = 'modern-blue'
|
self.style = 'modern-blue'
|
||||||
""" Head style: modern-blue, modern-green, modern-red and classic. """
|
""" Head style: modern-blue, modern-green, modern-red and classic. """
|
||||||
self.title = 'KiBot Bill of Materials'
|
self.title = 'KiBot Bill of Materials'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue