python讀取excel所有資料(cmd介面)

CodeMuscle發表於2020-05-14
python讀取excel所有資料(cmd介面)

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

相關文章