import click import pyperclip def _strip_parts(iterable): return [item.strip() for item in iterable] def _replace_empty_strings(iterable, replacement="None"): return [i or replacement for i in iterable] def prepare(text): lines = text.splitlines() # replacing the german decimal separator with the standard english dot correct_separator = (l.replace(",", ".") for l in lines) parted = (l.split("\t") for l in correct_separator) stripped = (_strip_parts(l) for l in parted) return [_replace_empty_strings(l) for l in stripped] def get_cell_lengths(table): # get the max lenght of each column in_columns = zip(*table) columns_lenghts = (map(len, c) for c in in_columns) return [max(c) for c in columns_lenghts] def pad_field(index, t): value, length = t if index == 0: return value.rjust(length) else: return value.ljust(length) def pad_fields(iterable, lengths): return [pad_field(i, t) for i, t in enumerate(zip(iterable, lengths))] def build_list(table, lengths): padded = (pad_fields(l, lengths) for l in table) padded_lines = (", ".join(l) for l in padded) lines_as_list = (f" [{l}], # noqa: E201, E202, E203, E501," for l in padded_lines) list_content = "\n".join(lines_as_list) return f"[\n{list_content}\n]\n" @click.command() def cli(): xls = pyperclip.paste() table = prepare(xls) lengths = get_cell_lengths(table) result = build_list(table, lengths) print("copied to clipboard") pyperclip.copy(result)