-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
83 lines (66 loc) · 2.96 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def load_data(filepath=None):
''' Retrieves and pack data from the excel database of archive resources '''
from xlrd import open_workbook
db = open_workbook("Lab-of-Fracture database.xlsx" if filepath is None else filepath).sheet_by_index(0)
keys = set()
for r in range(1, db.nrows-1):
keys.add(db.cell_value(r, 0))
archive = dict.fromkeys(keys, {})
keys = []
for h in range(2, db.ncols):
keys.append(db.cell_value(0, h).lower())
for f in archive.keys():
index = {}
for r in range(1, db.nrows-1):
if db.cell_value(r, 0) != f:
continue
document = {}
for i, h in enumerate(range(2, db.ncols)):
document[keys[i]] = str(db.cell_value(r, h)) if keys[i] not in ("pub_year", "vol_n", "paper_n") else str(db.cell_value(r, h))[:len(str(db.cell_value(r, h)))-2] if ".0" in str(db.cell_value(r, h)) else str(db.cell_value(r, h))
index[int(db.cell_value(r, 1))] = document
archive[f] = index
return archive
def generate_catalogs(archive):
''' Generates the requested Word file accordingly to all the authors formatting rules '''
from os import path, mkdir, getcwd
from datetime import datetime as dt
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
if not path.isdir(f'{getcwd()}\\reports'):
mkdir(f'{getcwd()}\\reports')
for folder, index in archive.items():
doc = Document()
doc.styles['Normal'].font.name = "Times New Roman"
doc.add_paragraph(f'FALDONE : {folder}')
for p in index.values():
par = doc.add_paragraph(f'{p["authors"]}: "{p["title"]}"', style="List Number")
if p["magazine_name"]:
par.add_run(f', {p["magazine_name"]}').italic = True
if p["vol_n"]:
par.add_run(f', Vol. {p["vol_n"]}')
if p["paper_n"]:
par.add_run(f', Paper N. {p["paper_n"]}')
if p["pub_year"]:
par.add_run(f'{", " if not p["vol_n"] else " "}({str(p["pub_year"])})')
if p["pp_interval"]:
par.add_run(f', {p["pp_interval"]}')
par.add_run('.')
par.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
doc.save(f'{getcwd()}\\reports\\CATALOGO {folder.upper()}.docx')
def install_packages():
from os import path, getcwd
from subprocess import call
if not path.isfile(f'{getcwd()}\\requirements.txt'):
print('ERROR : no dependencies register found in your distribution')
print('FIX ATTEMP : trying to retrive required packages ...')
call('pip freeze>requirements.txt', shell=True)
else:
call('pip install --upgrade -r requirements.txt', shell=True)
def setup():
from subprocess import call
from os import getcwd, path
if not path.isdir(f'{getcwd()}\\venv'):
call('python -m venv venv')
install_packages()
setup()
generate_catalogs(load_data())