61 lines
1.5 KiB
Python
Executable File
61 lines
1.5 KiB
Python
Executable File
#!/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.option("--count", type=int, default=200)
|
|
def random_table(rtype, seed, prec, min, max, cols, docx, py, count):
|
|
random.seed(seed)
|
|
|
|
rows = count // cols
|
|
if count % cols > 0:
|
|
rows += 1
|
|
|
|
rfunc = RFUNC[rtype]
|
|
array = [round(rfunc(min, max), prec) for _ in range(count)]
|
|
|
|
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):
|
|
if n >= count:
|
|
break
|
|
table.rows[i].cells[j].text = str(array[n])
|
|
n += 1
|
|
|
|
document.save(docx)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
cli()
|