commit d85a56f97c7dd012181587ea9e6b3ce9852f4e75 Author: Ilya Date: Wed Feb 28 11:56:55 2024 +0300 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18d920b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +venv/ +result/ + diff --git a/main.py b/main.py new file mode 100755 index 0000000..ddf83f9 --- /dev/null +++ b/main.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import random +import os + +import click +from docx import Document + + +RFUNC = { + "int": random.randint, + "float": random.uniform, +} + + +@click.group() +def cli(): + pass + + +@cli.command("random_table") +@click.option("--rtype", type=click.Choice(RFUNC.keys()), default="int") +@click.option("--seed", type=int, default=None) +@click.option("--prec", type=int, default=5) +@click.option("--min", type=float, default=0) +@click.option("--max", type=float, default=10) +@click.option("--cols", type=int, default=10) +@click.option("--docx", type=click.Path(dir_okay=False, writable=True, resolve_path=True), + default="./result/table.docx") +@click.option("--py", type=click.Path(dir_okay=False, writable=True, resolve_path=True, allow_dash=True), + default="./result/table.py") +@click.argument("size", type=int, default=200) +def random_table(rtype, seed, prec, min, max, cols, docx, py, size): + random.seed(seed) + + rows = size // cols + if size % cols > 0: + rows += 1 + + rfunc = RFUNC[rtype] + array = [round(rfunc(min, max), prec) for _ in range(size)] + + with click.open_file(py, "w") as f: + print(array, file=f) + + document = Document() + table = document.add_table(rows=rows, cols=cols) + n = 0 + for i in range(rows): + for j in range(cols): + table.rows[i].cells[j].text = str(array[n]) + n += 1 + + document.save(docx) + + +if __name__ == '__main__': + cli() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b4838c9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-docx +click + diff --git a/result/.gitkeep b/result/.gitkeep new file mode 100644 index 0000000..e69de29