代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import xlrd import os from prettytable import PrettyTable import pandas #创建一个Excel表类 class Excel( object ): def __init__( self , path): self .path = path / / 路径要加上文件名 #读取Excel内全部数据 参数sname是sheet页名字 def read_all_data( self , sname): workbook = xlrd.open_workbook( self .path) content = workbook.sheet_by_name(sname) # ord_list=[] ord_list = PrettyTable() for rownum in range (content.nrows): ord_list.add_row(content.row_values(rownum)) # ord_list.append(content.row_values(rownum)) #返回的类型是一个list return ord_list if __name__ = = "__main__" : path = input ( "-->>>Enter your path: " ) xl = pandas.ExcelFile(path) sheetsname = xl.sheet_names print ( "all your excel sheetsname: " + str (sheetsname)) # print(sheetsname) sheetname = input ( "-->>>Enter your excel sheet name: " ) path1 = Excel(path) alldata = path1.read_all_data(sname = sheetname) # alldata = PrettyTable(alldata) print (alldata) |
演示
加了prettytable美化显示
以上就是python在CMD界面读取excel所有数据的示例的详细内容,更多关于python读取excel数据的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/CodeMuscle/p/12887428.html