-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.py
65 lines (53 loc) · 1.83 KB
/
excel.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
#!/usr/bin/env python3
from logical_prop import LogicProposition, gen_table
import sys
if len(sys.argv) != 2:
from sys import stderr
print("Wrong number of arguments provided. Please provide the file name of the Excel file as the argument.", file=stderr)
exit(1)
p = None
if sys.stdin.isatty() and sys.stdout.isatty():
print(" ! is the symbol for not\n","| is the symbol for or\n","& is the symbol for and\n","> is the symbol for implies\n","= is the symbol for if and only if\n","Any upper case letter is an atomic proposition")
p = input(" Please input a string of symbols:\n")
else:
p = input()
prop = LogicProposition(p)
table = gen_table(prop)
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from itertools import count as icount
wb = Workbook()
sheet = wb.active
sheet.title = "Truth Table"
for row_index, line in zip(icount(1), table):
for column_name, item in zip((get_column_letter(i) for i in icount(1)), line):
try:
item = int(item)
except (ValueError, TypeError):
item = str(item)
if item == "T":
item = True
elif item == "F":
item = False
cell = sheet[column_name + str(row_index)]
cell.value = item
if row_index > 1 and column_name != 'A':
from openpyxl.styles import colors, Font, Color
ft = Font()
if item is True:
ft = Font(color=Color(rgb='00009242'))
elif item is False:
ft = Font(color=Color(rgb='00FF0000'))
cell.font = ft
if row_index == 1:
from openpyxl.styles.alignment import Alignment
cell.alignment = Alignment(horizontal='center')
sheet.freeze_panes = sheet['B2']
if sys.argv[1].strip() == "-":
from tempfile import NamedTemporaryFile
with NamedTemporaryFile("wb") as tmp:
wb.save(tmp.name)
tmp.seek(0)
sys.stdout.buffer.write(tmp.read())
else:
wb.save(sys.argv[1])