‘xlrd’ and ‘xlwt’ in Python
‘xlrd‘ and ‘xlwt‘ are two Python tripartite library for working with Excel files.
xlrd
Responsible for reading files.
Open up the Excel files
1
workbook = xlrd.open_workbook(path)
Get all sheets of file
1
workbook.sheet_names()
Get the sheet and content
1
2
3
4
5
6
7
8
9
10
11
12
13# Get sheet by index
sheet2 = workbook.sheet_by_index(1)
# Get sheet by name
sheet2 = workbook.sheet_by_name('sheet2')
# Get content
sheet2.name
sheet2.nrows
sheet2.ncols
sheet2.row_values(index)
sheet2.col_values(index)
xlwt
Responsible for write files.
Issue
Excel xlsx file not supported
Change the version of ‘xlrd’, it has been updated to 2.0.1 and only supports .xls files, not .xlsx
1
pip install xlrd==1.2.0
If file path has Chinese, ‘open’ operation needs to be transcoded.
- The prefix ‘r’ to path indicates the native string.
- filename = filename.decode(‘utf-8’)