Python xlrd读取excel日期类型

废话不多说,直接开讲!

今天遇到py读取excel日期问题

2021/10/11 10:27:55 这种格式

xldate_as_datetime #直接转化为datetime对象  
 succ_time = xldate_as_datetime(rows[3],0)  #成交时间

直接读取是科学计算法
在这里插入图片描述

处理结果
在这里插入图片描述
完成代码

# coding=utf-8
import math
import random
import time
from tkinter import filedialog, messagebox, ttk
import tkinter.messagebox

import requests
import xlrd
from tkinter.ttk import Label
from tkinter import *
import urllib3
import xlwt
from xlrd import xldate_as_datetime
from xlwt import Workbook
import threading
import tkinter as tk
urllib3.disable_warnings()
address = []
tb_order =""
erp_order =""
def main():
    def selectExcelfile():
        sfname = filedialog.askopenfilename(title='选择Excel文件', filetypes=[('Excel', '*.xls'), ('All Files', '*')])
        print(sfname)
        # 传递参数,目前参数固定值,可以设置以下动态参数
        # sku = e_1.get()
        num = e_2.get()
        # iscode = comboxlist.get()

        doProcess(sfname)
        e_3.insert(INSERT, sfname)

    def selectExcelfile1():
        sfname = filedialog.askopenfilename(title='选择Excel文件', filetypes=[('Excel', '*.xls'), ('All Files', '*')])
        # print(sfname)
        # 传递参数,目前参数固定值,可以设置以下动态参数
        # sku = e_1.get()
        num = e_2.get()
        # iscode = comboxlist.get()
        doProcess1(sfname)
        e_2.insert(INSERT, sfname)



    '''
    统计总数据
    '''

    def group(totals=None):
        print(totals)
        # totals = 1601
        s_r = math.ceil(totals / 500)-1
        print(s_r)
        arr_n = []
        nums = 0
        for n in range(1, s_r):
            nums += 1
            t_da = nums * 500
            arr_n.append(t_da)

        X = [x for x in range(1, totals)]
        print(arr_n)
        a = nums
        while a > 0:
            r = random.choice(arr_n)
            # print(arr_n)
            # print(r)
            if X[r] != 0 and X[r - 1] != 0 and X[r + 1] != 0:
                # print(r)
                X.insert(r, 0)
                a -= 1

        z = X.index(0)
        nn = 0
        while z:
            nn += 1
            t = tuple(X[:z])
            print(nn, X[:z])
            X = X[z + 1:]

            try:
                z = X.index(0)
            except ValueError:
                print(s_r, X)
                break

    def doProcess(sfname=None):
        global tb_order
        fname = sfname
        bk = xlrd.open_workbook(fname)
        shxrange = range(bk.nsheets)
        sh = bk.sheet_by_name("天猫订单商品导出")
        # 列数
        da = sh.nrows

        ncols = sh.ncols
        totals = da
        arr = []
        arrs =[]
        for ii in range(1, totals):
            rows = sh.row_values(ii)
            # 订单号
            order_no = rows[0].replace("’","")
            # 客户信息
            addr = rows[1]
            arr.append([order_no,addr])
        tb_order = arr




        # tkinter.messagebox.showinfo('提示', '处理Excel文件的添加成功。')
    def doProcess1(sfname=None):
        fname = sfname
        bk = xlrd.open_workbook(fname)
        table = bk.sheet_by_index(0)  # 按照索引读Excel文件
        shxrange = range(bk.nsheets)
        sh = bk.sheet_by_name("导出数据")
        # 列数
        da = sh.nrows

        ncols = sh.ncols
        totals = da
        global erp_order
        arr = []
        arrs = []
        for ii in range(1, totals):
            rows = sh.row_values(ii)
            # 原始订单号
            order_no = rows[0].replace(",","")
            orderno = rows[1]  # 订单号
            deal_time = rows[2] # 处理时间
            # succ_time = xlrd.xldate_as_tuple(rows[3], 0)
            # succ_time.datetime(2018, 7, 9, 0, 0)
            # succ_time = xldate_as_datetime(rows[3],0)  #成交时间
            succ_time = rows[3]
            net_name = rows[4] #网名
            re_name = rows[5] #收件人
            tel = rows[6] #电话
            addr = rows[7] #地址
            total_money = rows[8] #货款合计
            logic = rows[9] #物流方式
            logic_no = rows[10] #物流单号
            total_remark = rows[11] #合并备注
            add_remark = rows[12] #追加备注
            shopname = rows[13] #店铺名
            source = rows[14] #来源
            ke_remark = rows[15] #客户备注
            se_remark = rows[16] #客服备注
            order_status = rows[17] #订单状态
            reg = rows[18] #登记人
            xu_no = rows[19] #序号
            num_no = rows[20] #编号
            p_name = rows[21] #品名
            spec = rows[22] #规格
            unit = rows[23] #单位
            nums = rows[24] #数量
            price = rows[25] #单价
            money = rows[26] #金额
            remark = rows[27] #备注
            huo_remark = rows[28] #货品档案备注
            print(order_no,orderno,deal_time,succ_time,net_name,re_name,tel,addr,total_money,logic,logic_no,total_remark,add_remark,shopname,source,ke_remark,se_remark,order_status,reg,xu_no,
                  num_no,p_name,spec,unit,nums,price,money,remark,huo_remark)
            arr.append([order_no,orderno,deal_time,succ_time,net_name,re_name,tel,addr,total_money,logic,logic_no,total_remark,add_remark,shopname,source,ke_remark,se_remark,order_status,reg,xu_no,
                  num_no,p_name,spec,unit,nums,price,money,remark,huo_remark])
        erp_order = arr
            # print(order_no,orderno,deal_time,succ_time,net_name,re_name,tel,addr,total_money,logic,logic_no,total_remark,add_remark,shopname,source,ke_remark,se_remark,order_status,reg,xu_no,
            #       num_no,p_name,spec,unit,nums,price,money,remark,huo_remark)

        # 统计总数据,分组切割


        # print(s_r,888)
        arr_n = []
        nums = 0


        # tkinter.messagebox.showinfo('提示', '处理Excel文件的添加成功。')
    def log2():
        #
        # 判断编辑框的内容
        # messagebox.showinfo(title="提示",message="取消登录!")
        # if messagebox.askokcancel("确定", "取消操作!!"):
        root.destroy()

    def log1():
        #天猫订单
        sku = tb_order
        # erp订单
        erp = erp_order
        print(sku)
        print(erp)
        # iscode = comboxlist.get()
        # print(sku, num,iscode)


    # def go(*args):  # 处理事件,*args表示可变参数
        # print(comboxlist.get())  # 打印选中的值

    # 初始化
    root = Tk()

    # 设置窗体标题
    root.title('Excel 数据上传')

    # 设置窗口大小和位置
    root.geometry('550x300+570+200')
    f_1 = tk.Frame(root)
    f_1.place(x=10, y=80)
    # 标签1
    l_1 = tk.Label(f_1, text="erp文档订单:")
    l_1.pack()
    # 标签2
    # l_2 = tk.Label(f_1, text="数量:")
    # l_2.pack()
    # l_21 = tk.Label(f_1, text="代贴条码:")
    # l_21.pack()
    f_2 = tk.Frame(root)
    f_2.place(x=90, y=80)
    # f_3 = tk.Frame(root)
    # f_3.place(x=320, y=90)
    # 编辑框1
    # e_1 = tk.Entry(f_2, width=20)
    # e_1.pack()
    # f_5 = tk.Frame(root)
    # f_5.place(x=180, y=180)
    # 编辑框2,隐藏输入的内容
    e_2 = tk.Entry(f_2,  bg='red', width=50)
    e_2.pack()
    f_0 = tk.Frame(root)
    f_0.place(x=10, y=20)
    label1 = tk.Label(f_0, text='订单导出文档:')
    label1.pack()
    # label1 = tk.Frame(root)
    # label1.place(x=470, y=50)

    # text1 = Entry(root, bg='red', width=20)
    # text1.pack()
    text1 = tk.Frame(root)
    # text1.place(x=500, y=20)


    e_3 = tk.Entry(text1,  bg='red', width=50)
    e_3.pack()
    label1.pack()
    text1.pack()
    button1 = Button(root, text='浏览', width=8, command=lambda: thread_it(selectExcelfile))
    button1.pack()
    button1.place(x=450, y=13)
    button2 = Button(root, text='浏览', width=8, command=lambda: thread_it(selectExcelfile1))
    button2.pack()
    button2.place(x=450, y=80)

    # label1.place(x=30, y=30)
    text1.place(x=90, y=20)

    f_3 = tk.Frame(root)
    f_3.place(x=190, y=190)

    # 创建登陆按钮
    button = tk.Button(f_3, text="执行",width = 10, height = 1, command=log1, bg="green")
    button.pack()
    f_4 = tk.Frame(root)
    f_4.place(x=290, y=190)
    # 创建取消按钮
    button = tk.Button(f_4, text="取消",width = 10, height = 1, command=log2, bg="red")
    button.pack()
    comvalue = tk.Frame(root)  # 窗体自带的文本,新建一个值

    f_5 =  tk.Frame(root)  # 窗体自带的文本,新建一个值
    f_5.place(x=160,y=100)

    # comboxlist = ttk.Combobox(f_5, textvariable=comvalue)  # 初始化
    # comboxlist["values"] = ("是", "否")
    # comboxlist.current(0)  # 选择第一个
    # comboxlist.bind("<<ComboboxSelected>>", go)  # 绑定事件,(下拉列表框被选中时,绑定go()函数)
    # e_5 = tk.Entry(comboxlist, width=10)
    # comboxlist.pack()

    # e_5.pack()

    root.mainloop()

def thread_it(func, *args):
    '''将函数打包进线程'''
    # 创建
    t = threading.Thread(target=func, args=args)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
    # 阻塞--卡死界面!
    # t.join()

if __name__ == "__main__":
    main()


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值