forked from gnyers/python-tuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts.py
More file actions
executable file
·138 lines (113 loc) · 4.25 KB
/
ts.py
File metadata and controls
executable file
·138 lines (113 loc) · 4.25 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''Demonstration of working with spreadsheets
1. Worksheet management of a given spreadsheet:
1. list the sheets
CLI args: --list-sheets WORKBOOK
2. add sheet
CLI args: --addsheet SHEETNAME WORKBOOK
2. Data management:
1. dump the data on a sheet to the stdout or a given output file as text
CLI args: --dump SHEETNAME WORKBOOK
2. append provided CSV data at the end of a sheet
CLI args: --append CSVRECORD --sheet SHEETNAME WORKBOOK
3. CLI Interface: as described above
'''
### Import modules
import sys
import os.path
import argparse
import openpyxl
### Constants
DELIM = ';' # default delimiter of CSV data
### Function(s) for CLI arg. parsing
def parseargs(cmdline=sys.argv[1:], known_args_only=False):
p = argparse.ArgumentParser()
p.add_argument('-w', '--workbook', type=str, required=True,
help='File to read from (default: STDIN)')
sp = p.add_subparsers(help='commands')
# --- list shees
sp_list_sheets = sp.add_parser('list', help='List sheets')
sp_list_sheets.set_defaults(func=list_sheets)
# --- add sheet
sp_add_sheet = sp.add_parser('add', help='Add new sheets')
sp_add_sheet.add_argument('name', help='Name of the new sheet')
sp_add_sheet.set_defaults(func=add_sheet)
# --- dump sheet
sp_dump_sheet = sp.add_parser('dump', help='Dump data of sheet')
sp_dump_sheet.add_argument('name',
help='Name of the sheet to be dumped')
sp_dump_sheet.set_defaults(func=dump_data)
# --- add record
sp_add_rec = sp.add_parser('addrec', help='Add record to sheet')
sp_add_rec.add_argument('-n', '--name', type=str, default='',
help='Name of the sheet to add record to')
sp_add_rec.add_argument('-d', '--delim', type=str, default=DELIM,
help='CSV delimiter')
sp_add_rec.add_argument('record', type=str,
help='Add this record to sheet')
sp_add_rec.set_defaults(func=add_rec)
if known_args_only:
return p.parse_known_args(cmdline)[0] # interested in known args only
else:
return p.parse_args(cmdline) # parse all args!
### Other functions
def wb_open(fname):
try:
wb = openpyxl.load_workbook(fname)
except Exception as e:
print('Can not open workbook. Aborting', file=sys.stderr)
print(e, file=sys.stderr)
sys.exit(10)
return wb
### Functions implementing requirements
def list_sheets(workbook):
''' List the sheets in *workbok*
'''
return [ sheet.title for sheet in workbook.worksheets ]
def add_sheet(workbook, name, index=0):
''' Create new sheet with *name* in *workbook* at position *index*
'''
workbook.create_sheet(title=name, index=index)
return workbook
def dump_data(sheet):
''' Dump all data of *sheet*
'''
return [[cell.value for cell in row ] for row in sheet[sheet.dimensions]]
def add_rec(sheet, data):
''' Append *data* after the last record in *sheet*
'''
# row_nr = len(sheet.row_dimensions)
last_row_first_cell = list(sheet.iter_rows())[-1][0]
next_row = last_row_first_cell.row + 1
next_col = last_row_first_cell.column
for col, value in enumerate(data, next_col):
sheet.cell(row=next_row, column=col, value=value)
return sheet
### main starts here
if __name__ == '__main__':
args = parseargs()
wb = wb_open(args.workbook)
if args.func == list_sheets:
for s in list_sheets(wb): print(s)
if args.func == add_sheet:
wb = add_sheet(workbook=wb, name=args.name)
wb.save(args.workbook)
if args.func == dump_data:
try:
ws = wb[args.name]
data = dump_data(ws)
print(data)
except KeyError as e:
print(e.args[0], file=sys.stderr)
sys.exit(20)
if args.func == add_rec:
try:
rec = args.record.split(args.delim)
ws = wb[args.name] if args.name else wb.active
add_rec(ws, rec)
wb.save(args.workbook)
print('Added record to sheet: {}'.format(ws.title))
except KeyError as e:
print(e.args[0], file=sys.stderr)
sys.exit(30)