-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheetToTextFiles.py
43 lines (27 loc) · 1022 Bytes
/
spreadsheetToTextFiles.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
#! python3
# spreadsheetToTextFiles.py - Reads the contents of spreadsheet,
# and saves content into text file(s), one row each line and one file each column.
import openpyxl, sys
if len(sys.argv) == 2:
try:
wb = openpyxl.load_workbook(sys.argv[1])
except Exception as e:
print(e)
sheet = wb.active
# Treat each column as a its own new text file.
for c in range(1, sheet.max_column + 1):
# First row is treated as a header.
file_name = sheet.cell(row = 1, column = c).value + '.txt'
text_file = open(file_name, 'w')
lines = []
# For all subsequent rows after the header.
for r in range(2, sheet.max_row + 1):
v = sheet.cell(row = r, column = c).value
# Check if cell has content, then add to list of lines.
if v is not None:
lines.append(v)
text_file.write('\n'.join(lines))
print('Spreadsheet lines from ' + file_name[:-4] + ' successfully saved as ' + file_name)
text_file.close()
else:
print('You must include a .xlsx file name in your argument.')