Python xlrd 读取excel表格 常用用法整理
生活随笔
收集整理的這篇文章主要介紹了
Python xlrd 读取excel表格 常用用法整理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
xlrd 的使用?
#!/usr/bin/python#
# -*- coding: utf-8 -*-
import xlrd
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
# 打開excel
table = xlrd.open_workbook('/home/hly/hly/test.xls')
# excel 地步表格的名稱
sheetName = table.sheet_names()
print(sheetName)
# 獲取sheet對象
print (table.sheets())
# 獲取sheet總數
print(table.nsheets)
# 通過索引獲取,例如打開第二個sheet表格
sheet = table.sheet_by_index(1)
print (sheet)
# 通過sheet名稱獲取,例如打開第二個sheet表格
# sheet = table.sheet_by_name("Sheet")
# print (sheet)
# 獲取名稱
print sheet.name
# 獲取行數
rows = sheet.nrows
print (rows)# 獲取列數
cols = sheet.ncols
print (cols)# 獲取第一行的數據
one_content = str(sheet.row_values(0)).replace('u\'', '\'').decode("unicode-escape")
print (one_content)
# 獲取第一行表格的內容以及內容的類型
one_content = str(sheet.row(0)).replace('u\'', '\'').decode("unicode-escape")
print (one_content)# 獲取第一列的數據
one_col_content = str(sheet.col_values(0)).replace('u\'', '\'').decode("unicode-escape")
print one_col_content# 單元格的類型
table_content = sheet.cell_type(2,2)
print (table_content)# 單元格的內容和類型
table_content = sheet.cell(2, 2)
print (table_content)# 單元格的內容
table_content = sheet.cell_value(2, 2)
print (table_content)# 根據行數遍歷出整個表格
content_list = []
for i in range(rows):content_list.append(sheet.row_values(i))
# 處理list中文亂碼
case_list = str(content_list).replace('u\'', '\'').decode("unicode-escape")
print (case_list)
xlrd 的注意事項
當使用xlrd 讀取excle 表格的時候需要注意xlrd 的版本
xlrd 版本低于或者等于1.2.0 的時候 讀取excel 不支持中文名稱的excel 但是確實 以.xls 和 .xlsx 結尾的 excel
xlrd 版本是最新版本的時候目前最新的版本為2.0.1 支持中為名稱的excel 但是 不支持.xlsx 結尾的excel ,只支持.xls結尾的excel
?
總結
以上是生活随笔為你收集整理的Python xlrd 读取excel表格 常用用法整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 丰田凯美瑞车的缺点和优点是什么??
- 下一篇: 求一个好听的将军名字