Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 386e29e

Browse files
committed
text files to columns in excel worksheet
1 parent 0566267 commit 386e29e

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
text1
2+
tex1, 1
3+
tex1, 2
4+
text1, 3
5+
text1, 4
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
text2
2+
text2, 1
3+
text2, 2
4+
text2, 3
5+
text2, 4
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
text3
2+
text3, 1
3+
text3, 2
4+
text3, 3
5+
text3, 4
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import openpyxl
3+
4+
5+
def textToSheet(directory, filename):
6+
"""converts text files to columns in excel worksheet
7+
Args:
8+
directory (str): folder containing text files
9+
filename (str): name of excel file
10+
Returns:
11+
None
12+
"""
13+
wb = openpyxl.Workbook()
14+
wb.create_sheet(index=0, title='result')
15+
sheet = wb.active
16+
17+
colIndex = 1
18+
19+
# write text files as columns in worksheet
20+
for file in os.listdir():
21+
if file.endswith('.txt'):
22+
rowIndex = 1
23+
with open(file) as f:
24+
for line in f:
25+
sheet.cell(row=rowIndex, column=colIndex).value = line
26+
rowIndex += 1
27+
colIndex += 1
28+
29+
wb.save(filename)
30+
31+
if __name__ == "__main__":
32+
textToSheet('.', 'text-to-cols.xlsx')

0 commit comments

Comments
 (0)