python:xlrd 读取 Excel文件,显示在 tkinterTable 表格中

pip install xlrd

xlrd-1.2.0-py2.py3-none-any.whl (103 kB)

摘要: Library for developers to extract data from Microsoft Excel (tm) spreadsheet files

pip install tkinterTable

tkintertable-1.3.3.tar.gz (58 kB)

摘要: Extendable table class for Tkinter

源代码链接:https://github.com/dmnfarrell/tkintertable

教程链接:https://github.com/dmnfarrell/tkintertable/wiki/Usage

编写 tk_table_xlsx.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" xlrd 读取 Excel文件,显示在 tkinterTable 表格中"""
import os
import sys
import datetime
import tkinter as tk
from tkinter import filedialog
import xlrd
from tkintertable import TableCanvas, TableModel

root = tk.Tk()
filetypes = [('Excel file','.xlsx')]
file1 = filedialog.askopenfilename(initialdir=".", filetypes=filetypes)
if file1 == '':
    print('usage: python tk_table_xlsx.py ')
    sys.exit(1)    

if not os.path.exists(file1):
    print(f'Error: {file1} not found.')
    sys.exit(2)

book = xlrd.open_workbook(file1)
sheet = book.sheets()[0] # 取Sheet1
# 通过名称获取
#sheet = book.sheet_by_name(sheet_name)
nrows = sheet.nrows  # 获取sheet中的有效行数
if nrows > 10000:
    print(f" 行数: {nrows} > 10000 !")
    nrows = 10000
ncols = sheet.ncols  # 获取sheet中的有效列数
if ncols > 26:
    print(f" columns: {ncols} > 26 !")
    ncols = 26

data = {}
cols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#单元类型cell_type: empty为0, string为1, number为2, date为3, boolean为4, error为5
for i in range(0, nrows):
    rows = {}
    for j in range(0, ncols):
        v = sheet.cell_value(i,j)
        t = sheet.cell_type(i,j)
        if t ==0:
            rows[cols[j]] = ''
        elif t ==1:
            rows[cols[j]] = v
        elif t ==2:
            if v%1 ==0:
                rows[cols[j]] = "%d" % v
            else:
                rows[cols[j]] = "%.4f" % v
        elif t ==3:
            date_tuple = xlrd.xldate_as_tuple(v, book.datemode)
            date_v = datetime.date(*date_tuple[:3])
            rows[cols[j]] = date_v.strftime('%Y-%m-%d')
        else:
            rows[cols[j]] = str(v)
    data[str(i+1)] = rows
#print(data)

#root = tk.Tk()
root.title(file1)
root.geometry("800x400")
frame = tk.Frame(root)
frame.pack()

model = TableModel()
table = TableCanvas(frame, model=model, data=data)
table.show()

root.mainloop()

运行 python tk_table_xlsx.py test1.xlsx

参考:python实现读取excel表格详解方法

参考:使用tkintertable控件建立tkinter的表格

相关推荐
終不似少年遊*几秒前
国产之光DeepSeek架构理解与应用分析04
人工智能·python·深度学习·算法·大模型·ds
明月看潮生9 分钟前
青少年编程与数学 02-016 Python数据结构与算法 28课题、图像处理算法
图像处理·python·算法·青少年编程·编程与数学
路有瑶台32 分钟前
EXCEL学习
学习·excel
羊小猪~~43 分钟前
深度学习基础--CNN经典网络之InceptionV3详解与复现(pytorch)
网络·人工智能·pytorch·python·深度学习·机器学习·cnn
深度学习lover1 小时前
<项目代码>YOLO小船识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·小船识别
愚公搬代码2 小时前
【愚公系列】《Python网络爬虫从入门到精通》055-Scrapy_Redis分布式爬虫(安装Redis数据库)
数据库·爬虫·python
浅浅2802 小时前
numpy、pandas内存优化操作整理
数据结构·经验分享·python·学习·性能优化·numpy·pandas
拓端研究室TRL2 小时前
Python+AI提示词比特币数据预测:Logistic逻辑回归、SVC及XGB特征工程优化实践
开发语言·人工智能·python·算法·逻辑回归
就叫飞六吧2 小时前
Python自动化selenium-一直卡着不打开浏览器怎么办?
python·selenium·自动化
亚林瓜子3 小时前
AWS Elastic Beanstalk的部署Python Flask后端服务(Hello,World)
python·flask·aws·eb