This commit is contained in:
Ilya Bezrukov 2024-02-28 11:56:55 +03:00
commit d85a56f97c
4 changed files with 65 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.idea/
venv/
result/

58
main.py Executable file
View File

@ -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()

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
python-docx
click

0
result/.gitkeep Normal file
View File