当前位置:主页 > 软件编程 > Python代码 >

python在CMD界面读取excel所有数据的示例

时间:2020-09-28 22:33:47 | 栏目:Python代码 | 点击:

代码

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美化显示

您可能感兴趣的文章:

相关文章