-
Notifications
You must be signed in to change notification settings - Fork 12
/
Markdown-Table-Generator.py
101 lines (93 loc) · 3.47 KB
/
Markdown-Table-Generator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
print("**** Welcome to Markdown Table Generator ****")
print("Press 1 : If you already have a csv file :")
print("Press 2 : If you want to create a table :")
ch = input("> ")
if ch=='1':
name = input("Enter a csv file name: ")
center=input("Do you want items to be centered? (true/false): ")
input_file = open(f"{name}.csv", "r")
output_file = open("table.md", "w")
i=0
for line in input_file:
arr = line.split(",")
j=0
if i==0: # For headings
for each in arr:
if j==len(arr)-1:
each = each.rstrip('\n')
entry= f"| \t{each}\t | \n"
else:
entry= f"| \t{each}\t "
output_file.write(entry)
j=j+1
for j in range(0, len(arr)):
if j==len(arr)-1:
if center=="true" and len(each)>2:
entry= f"| \t:{'-'*(len(each)-2)}:\t | \n"
else:
entry= f"| \t{'-'*len(each)}\t | \n"
else:
if center=="true" and len(each)>2:
entry= f"| \t:{'-'*(len(each)-2)}:\t "
else:
entry= f"| \t{'-'*len(each)}\t "
output_file.write(entry)
j=j+1
else: # For remaining rows
for each in arr:
if j==len(arr)-1:
each = each.rstrip('\n')
entry= f"| \t{each}\t | \n"
else:
entry= f"| \t{each}\t"
output_file.write(entry)
j=j+1
i=i+1
input_file.close()
output_file.close()
elif ch=='2':
rows=int(input("Enter number of rows: "))
cols=int(input("Enter number of columns: "))
center=input("Do you want items to be centered? (true/false): ")
headings=[]
print("\nEnter the data for each cell respectively!")
file = open("table.md", "w")
print("Let's Generate a Table in Markdown Syntax:\n")
for i in range(1, cols+1):
value=input(f"Enter Heading for column{i} : ")
if i==cols:
entry= f"| {value} |\n"
headings.append(value)
else:
entry= f"| {value} "
headings.append(value)
file.write(entry)
print("Headings Received!")
for i in range(0, cols):
if i==cols-1:
if center=="true" and len(headings[i])>2:
entry= f"| :{'-'*(len(headings[i])-2)}: |\n"
else:
entry= f"| {'-'*len(headings[i])} |\n"
else:
if center=="true" and len(headings[i])>2:
entry= f"| :{'-'*(len(headings[i])-2)}: "
else:
entry= f"| {'-'*len(headings[i])} "
file.write(entry)
print("Done!")
for i in range(2, rows+1):
print(f"\nEnter data for Row{i} : ")
for j in range(1, cols+1):
value=input(f"Row{i} Column{j} - {headings[j-1]}: ")
if j==cols:
entry= f"| {value} |\n"
file.write(entry)
else:
entry= f"| {value} "
file.write(entry)
file.close()
else:
print("Wrong input!")
print("\nDone Succesfully! :)")
print("Now check table.md or run 'cat table.md' in terminal to copy the syntax.\n")