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的表格

相关推荐
waterHBO1 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio6 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal8 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ8 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.9 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~9 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算