From 77bf713b26212171baf947b1b767df05441a3d80 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Thu, 6 Aug 2020 09:04:16 -0300 Subject: [PATCH] Max number of chars per column configurable in XLSX. Also added row height adjust. --- kiplot/bom/xlsx_writer.py | 23 ++++++++++++++++++----- kiplot/out_bom.py | 7 ++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/kiplot/bom/xlsx_writer.py b/kiplot/bom/xlsx_writer.py index b9e4964b..b68d723a 100644 --- a/kiplot/bom/xlsx_writer.py +++ b/kiplot/bom/xlsx_writer.py @@ -5,8 +5,8 @@ This code is adapted from https://github.com/SchrodingersGat/KiBoM by Oliver Hen Generates an XLSX file. """ -# TODO: alternate colors, set row height, put logo, move data to beginning import io +from textwrap import wrap from base64 import b64decode from .columnlist import ColumnList from .kibot_logo import KIBOT_LOGO @@ -21,7 +21,6 @@ except ModuleNotFoundError: pass logger = log.get_logger(__name__) -MAX_WIDTH = 60 BG_GEN = "#E6FFEE" BG_KICAD = "#FFE6B3" BG_USER = "#E6F9FF" @@ -207,6 +206,9 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg): head_size = compute_head_size(cfg) # First rowe for the information 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 @@ -242,6 +244,7 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg): # Headings # Create the head titles column_widths = [0]*len(col_fields) + rows = [row_headings] for i in range(len(row_headings)): # Title for this column column_widths[i] = len(row_headings[i]) + 10 @@ -254,6 +257,7 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg): continue # Get the data row row = group.get_row(col_fields) + rows.append(row) if link_datasheet != -1: datasheet = group.get_field(ColumnList.COL_DATASHEET_L) # Fill the row @@ -286,7 +290,6 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg): # PCB & Stats Info if not (cfg.xlsx.hide_pcb_info and cfg.xlsx.hide_stats_info): rc = r_info_start - # TODO: Language? 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, "Variant:", ' + '.join(cfg.variant)) @@ -305,10 +308,20 @@ def write_xlsx(filename, groups, col_fields, head_names, cfg): # Adjust the widths for i in range(len(column_widths)): width = column_widths[i] - if width > MAX_WIDTH: - width = MAX_WIDTH + if width > max_width: + width = max_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.repeat_rows(head_size+1) worksheet.set_landscape() diff --git a/kiplot/out_bom.py b/kiplot/out_bom.py index 2e9dd3f0..9b4b1f2c 100644 --- a/kiplot/out_bom.py +++ b/kiplot/out_bom.py @@ -28,9 +28,8 @@ class BoMRegex(Optionable): self.regexp = None """ {regex} """ # pragma: no cover - def __str__(self): - # TODO make a list - return self.column+'\t'+self.regex +# def __str__(self): +# return self.column+'\t'+self.regex class BoMColumns(Optionable): @@ -146,6 +145,8 @@ class BoMXLSX(Optionable): """ Hide statistics information """ self.logo = Optionable """ [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' """ Head style: modern-blue, modern-green, modern-red and classic. """ self.title = 'KiBot Bill of Materials'