日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python import xlrd 报错_python读取excel(xlrd)

發布時間:2024/4/11 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python import xlrd 报错_python读取excel(xlrd) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、安裝xlrd模塊:

1、mac下打開終端輸入命令:

pip install xlrd

2、驗證安裝是否成功:

在mac終端輸入 python ?進入python環境

然后輸入 import xlrd

不報錯說明模塊安裝成功

二、常用方法:

1、導入模塊:

import xlrd

2、打開文件:

x1 = xlrd.open_workbook("data.xlsx")

3、獲取sheet:

獲取所有sheet名字:x1.sheet_names()

獲取sheet數量:x1.nsheets

獲取所有sheet對象:x1.sheets()

通過sheet名查找:x1.sheet_by_name("test”)

通過索引查找:x1.sheet_by_index(3)

# -*- coding:utf-8 -*-

import xlrd

import os

filename = "demo.xlsx"

filePath = os.path.join(os.getcwd(), filename)

print filePath

# 1、打開文件

x1 = xlrd.open_workbook(filePath)

# 2、獲取sheet對象

print 'sheet_names:', x1.sheet_names() # 獲取所有sheet名字

print 'sheet_number:', x1.nsheets # 獲取sheet數量

print 'sheet_object:', x1.sheets() # 獲取所有sheet對象

print 'By_name:', x1.sheet_by_name("test") # 通過sheet名查找

print 'By_index:', x1.sheet_by_index(3) # 通過索引查找

輸出:

sheet_names: [u' plan', u'team building', u'modile', u'test']

sheet_number: 4

sheet_object: [, , , ]

By_name:

By_index:

4、獲取sheet的匯總數據:

獲取sheet名:sheet1.name

獲取總行數:sheet1.nrows

獲取總列數:sheet1.ncols

# -*- coding:utf-8 -*-

import xlrd

import os

from datetime import date,datetime

filename = "demo.xlsx"

filePath = os.path.join(os.getcwd(), filename)

print filePath

# 打開文件

x1 = xlrd.open_workbook(filePath)

# 獲取sheet的匯總數據

sheet1 = x1.sheet_by_name("plan")

print "sheet name:", sheet1.name # get sheet name

print "row num:", sheet1.nrows # get sheet all rows number

print "col num:", sheet1.ncols # get sheet all columns number

輸出:

sheet name: plan

row num: 31

col num: 11

5、單元格批量讀取:

a)行操作:

sheet1.row_values(0)? # 獲取第一行所有內容,合并單元格,首行顯示值,其它為空。

sheet1.row(0)????????   # 獲取單元格值類型和內容

sheet1.row_types(0)?? # 獲取單元格數據類型

# -*- coding:utf-8 -*-

import xlrd

import os

from datetime import date,datetime

filename = "demo.xlsx"

filePath = os.path.join(os.getcwd(), filename)

x1 = xlrd.open_workbook(filePath)

sheet1 = x1.sheet_by_name("plan")

# 單元格批量讀取

print sheet1.row_values(0) # 獲取第一行所有內容,合并單元格,首行顯示值,其它為空。

print sheet1.row(0) # 獲取單元格值類型和內容

print sheet1.row_types(0) # 獲取單元格數據類型

輸出:

[u'learning plan', u'', u'', u'', u'', u'', u'', u'', 123.0, 42916.0, 0]

[text:u'learning plan', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', number:123.0, xldate:42916.0, bool:0]

array('B', [1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4])

b) 表操作

sheet1.row_values(0, 6, 10)?? # 取第1行,第6~10列(不含第10表)

sheet1.col_values(0, 0, 5)??? # 取第1列,第0~5行(不含第5行)

sheet1.row_slice(2, 0, 2)???? # 獲取單元格值類型和內容

sheet1.row_types(1, 0, 2)?? # 獲取單元格數據類型

# -*- coding:utf-8 -*-

import xlrd

import os

from datetime import date,datetime

filename = "demo.xlsx"

filePath = os.path.join(os.getcwd(), filename)

print filePath

# 1、打開文件

x1 = xlrd.open_workbook(filePath)

sheet1 = x1.sheet_by_name("plan")

# 列操作

print sheet1.row_values(0, 6, 10) # 取第1行,第6~10列(不含第10表)

print sheet1.col_values(0, 0, 5) # 取第1列,第0~5行(不含第5行)

print sheet1.row_slice(2, 0, 2) # 獲取單元格值類型和內容,同sheet1.row(0)

print sheet1.row_types(1, 0, 2) # 獲取單元格數據類型

輸出:

[u'', u'', 123.0, 42916.0]

[u'learning plan', u'\u7f16\u53f7', 1.0, 2.0, 3.0]

[number:1.0, text:u'\u7ba1\u7406\u5b66\u4e60']

array('B', [1, 1])

6、特定單元格讀取:

a) 獲取單元格值:

sheet1.cell_value(1, 2)

sheet1.cell(1, 2).value

sheet1.row(1)[2].value

b) 獲取單元格類型:

sheet1.cell(1, 2).ctype

sheet1.cell_type(1, 2)

sheet1.row(1)[2].ctype

# -*- coding:utf-8 -*-

import xlrd

import os

from datetime import date,datetime

filename = "demo.xlsx"

filePath = os.path.join(os.getcwd(), filename)

x1 = xlrd.open_workbook(filePath)

sheet1 = x1.sheet_by_name("plan")

# 特定單元格讀取

# 取值

print sheet1.cell_value(1, 2)

print sheet1.cell(1, 2).value

print sheet1.row(1)[2].value

#取類型

print sheet1.cell(1, 2).ctype

print sheet1.cell_type(1, 2)

print sheet1.row(1)[2].ctype

7、(0,0)轉換A1:

xlrd.cellname(0, 0)?? # (0,0)轉換成A1

xlrd.cellnameabs(0, 0) # (0,0)轉換成$A$1

xlrd.colname(30)? # 把列由數字轉換為字母表示

# -*- coding:utf-8 -*-

import xlrd

import os

filename = "demo.xlsx"

filePath = os.path.join(os.getcwd(), filename)

# 打開文件

x1 = xlrd.open_workbook(filePath)

sheet1 = x1.sheet_by_name("plan")

# (0,0)轉換成A1

print xlrd.cellname(0, 0) # (0,0)轉換成A1

print xlrd.cellnameabs(0, 0) # (0,0)轉換成$A$1

print xlrd.colname(30) # 把列由數字轉換為字母表示

輸出:

A1

$A$1

AE

8、數據類型:

空:0

字符串:1

數字:2

日期:3

布爾:4

error:5

總結

以上是生活随笔為你收集整理的python import xlrd 报错_python读取excel(xlrd)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。