python excle读

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/4/24 9:57
# @File    : Excel读.py
# @Software: PyCharm

"""
excle处理有三个层次:文件,sheet页,行和列
打开excle文件,获取对应sheet页,获取对应sheet页的行或列的数据
xlsx类型文件用openpysl库操作
xls类型文件用xlrd库读取,xlwt库写
"""

import xlrd,xlwt
import os

#打开一个workbook
data = xlrd.open_workbook(r"C:\Users\6396000951\Desktop\相同Item存在ECCN不一致的Item.xls")

#抓取所有sheet页的名称
# worksheets = data.sheet_names()
# print('worksheets is %s' %worksheets)

#定位到sheet1
table = data.sheet_by_index(0)
item = table.col_values(1)[1:]
print(item)
l = list(item)
l1 = []
for id in l:
    if id not in l1:
        l1.append(id)
print(l1)
print(len(l1))

"""
#通过索引顺序获取
worksheet1 = workbook.sheets()[0]
#或
worksheet1 = workbook.sheet_by_index(0)
"""

"""
#遍历所有sheet对象
for worksheet_name in worksheets:
worksheet = workbook.sheet_by_name(worksheet_name)
"""

#遍历sheet1中所有行row
'''
num_rows = worksheet1.nrows
for curr_row in range(num_rows):
    row = worksheet1.row_values(curr_row)
    print('row%s is %s' %(curr_row,row))
'''


#遍历sheet1中所有列col
'''
num_cols = worksheet1.ncols
for curr_col in range(num_cols):
    col = worksheet1.col_values(curr_col)
    print('col%s is %s' %(curr_col,col))
'''


#遍历sheet1中所有单元格cell
'''
for rown in range(num_rows):
    for coln in range(num_cols):
        cell = worksheet1.cell_value(rown,coln)
        print(cell)
'''

"""
#其他写法:
cell = worksheet1.cell(rown,coln).value
print cell
#或
cell = worksheet1.row(rown)[coln].value
print cell
#或
cell = worksheet1.col(coln)[rown].value
print cell
#获取单元格中值的类型,类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
cell_type = worksheet1.cell_type(rown,coln)
print cell_type
"""

猜你喜欢

转载自www.cnblogs.com/wangdecheng/p/10760835.html
今日推荐