Skip to content

Commit 40dc9ee

Browse files
author
w3x10e8
authored
Read-Write Excel files with python
Solving Issue #358
1 parent 9bc756f commit 40dc9ee

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

read_excel_file.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# extract number of rows using Python
2+
import xlrd
3+
4+
# Give the location of the file
5+
loc = ("sample.xlsx")
6+
wb = xlrd.open_workbook(loc)
7+
sheet = wb.sheet_by_index(0)
8+
sheet.cell_value(0, 0)
9+
# Extracting number of rows
10+
print(sheet.nrows)
11+
12+
13+
14+
# extract number of columns in Python
15+
print(sheet.ncols)
16+
17+
18+
# extracting all columns name in Python
19+
for i in range(sheet.ncols):
20+
print(sheet.cell_value(0, i))
21+
22+
23+
#extracting first column
24+
sheet = wb.sheet_by_index(0)
25+
for i in range(sheet.nrows):
26+
print(sheet.cell_value(i, 0))
27+
28+
#extract a particular row value
29+
sheet = wb.sheet_by_index(0)
30+
print(sheet.row_values(1))

sample.xlsx

81.5 KB
Binary file not shown.

write_excel_file.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Writing to an excel
2+
# sheet using Python
3+
import xlwt
4+
from xlwt import Workbook
5+
6+
# Workbook is created
7+
wb = Workbook()
8+
9+
# add_sheet is used to create sheet.
10+
sheet1 = wb.add_sheet('Sheet 1')
11+
12+
sheet1.write(1, 0, 'ISBT DEHRADUN')
13+
sheet1.write(2, 0, 'SHASTRADHARA')
14+
sheet1.write(3, 0, 'CLEMEN TOWN')
15+
sheet1.write(4, 0, 'RAJPUR ROAD')
16+
sheet1.write(5, 0, 'CLOCK TOWER')
17+
sheet1.write(0, 1, 'ISBT DEHRADUN')
18+
sheet1.write(0, 2, 'SHASTRADHARA')
19+
sheet1.write(0, 3, 'CLEMEN TOWN')
20+
sheet1.write(0, 4, 'RAJPUR ROAD')
21+
sheet1.write(0, 5, 'CLOCK TOWER')
22+
23+
wb.save('xlwt example.xls')

0 commit comments

Comments
 (0)