首页 > 编程语言 > python在CMD界面读取excel所有数据的示例
2020
10-10

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

代码

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数据的资料请关注自学编程网其它相关文章!

编程技巧