我用Python + PAGE(Tkinter GUI) + pandas做了个企查查导出模板转机器人模板的小工具

产品需求

企查查导出的Excel文件是这样的
在这里插入图片描述
客户的机器人软件需要导入的模板是这样的
在这里插入图片描述
简单说就是要做个小工具将企查查导出的Excel通过pandas分析整理后按照客户给出的模板表头生成新的Excel文件。

效果图

成品效果图

在这里插入图片描述

PAGE Gui效果图

因为底部的statusbar无法通过gui设置,需要在生成的python中加入如下代码:

statusbar = tk.Label(root, text="技术支持:山东中达物联科技有限公司 18678709906", bd=1, relief=tk.SUNKEN, anchor=tk.W)
statusbar.pack(side=tk.BOTTOM, fill=tk.X)

在这里插入图片描述

代码实现

项目简单,都写到main.py一个文件里了,代码如下:

import os
# import sys
import platform
import subprocess
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

output_file_name = '导出客户模板.xlsx'
desktop_path = os.path.join(os.path.expanduser('~'), "Desktop")


# system_os = platform.system()


# 导入文件
def set_upload_file():
    # askopenfilename 1次上传1个;askopenfilenames1次上传多个
    select_file = filedialog.askopenfilename(
        filetypes=[('Excel 文件', '*.xls;*.xlsx'), ('Excel 文件', '*.xlsx'), ('Excel 97-2003文件', '*.xls')])
    file_path.set(select_file)


# 企查查电话排序
def qcc_sort(arr):
    arr1 = []
    arr2 = []
    for v in arr:
        if v.find('1') == 0:
            arr1.append(v)
        elif v.find('0') == 0:
            arr2.append(v)
    return arr1 + arr2


# 设置导出路径
def set_output_path():
    path = filedialog.askdirectory()
    output_path.set(path + os.sep + output_file_name)


# 导出文件
def output_file(file, product, output):
    # pass
    # file_path = r'./file/企查查导出.xls'
    try:
        df = pd.read_excel(file, header=1)
    except Exception as e:
        messagebox.showerror('打开转换文件失败', f'打开转换文件失败,请检查文件是否无误{e}')
    else:
        df_out = pd.DataFrame()
        check_col = {'法定代表人', '电话', '更多电话', '企业名称', '企业地址', '经营范围'}
        if check_col.issubset(df.columns) == False:
            messagebox.showwarning('文件错误', '该转换文件不是标准的企查查导出表格,请检查后重新导入')
        else:
            df_out['客户名称'] = df['法定代表人']
            df_out['phones'] = df['电话'] + ';' + df['更多电话']
            df_out['手机'] = ''
            df_out['手机2'] = ''
            df_out['公司名称'] = ''
            df_out['所属工号'] = ''
            df_out['手机3'] = ''
            df_out['备注'] = ''
            df_out['手机4'] = ''
            df_out['手机5'] = ''
            df_out['手机6'] = ''
            df_out['微电同号'] = ''
            df_out['修改电话'] = ''
            df_out['手机7'] = ''
            df_out['介绍电话1'] = ''
            df_out['介绍电话2'] = ''
            df_out['有效电话'] = ''
            df_out['产品'] = product
            df_out['单位'] = df['企业名称']
            df_out['地址'] = df['企业地址']
            df_out['经营范围'] = df['经营范围']
            df_out['phones'] = df_out['phones'].str.replace(';-', '')
            df_out['phones'] = df_out['phones'].str.replace('-', '')
            # 遍历列
            for index, row in df_out.iterrows():
                phones = row['phones'].split(';')
                # 倒叙排列 固话在手机号后边
                # phones.sort(reverse=True)
                # print(phones)
                phones = qcc_sort(phones)
                # print(phones)
                i = len(phones)
                for phone in phones:
                    if i >= 1:
                        row['手机'] = phones[0]
                    if i >= 2:
                        row['手机2'] = phones[1]
                    if i >= 3:
                        row['手机3'] = phones[2]
                    if i >= 4:
                        row['手机4'] = phones[3]
                    if i >= 5:
                        row['手机5'] = phones[4]
                    if i >= 6:
                        row['手机6'] = phones[5]
                    if i >= 7:
                        row['手机7'] = phones[6]

            # 删除临时列并导出
            df_out = df_out.drop('phones', axis=1)
            try:
                df_out.to_excel(output, index=False)
            except Exception as e:
                messagebox.showerror('保存文件失败', f'保存表格文件失败,请确认文件是否被打开!\n 详细错误信息:错误号-{e.errno} [{e.strerror}]')
            else:
                messagebox.showinfo('保存成功', '保存文件成功,点击确定打开文件')
                if platform.system() == 'Windows':
                    os.startfile(output)
                elif platform.system() == 'Linux':
                    subprocess.call(["xdg-open", output])
                elif platform.system() == 'Darwin':
                    subprocess.call(["open", output])


window = tk.Tk()
# 获取桌面路径
output_path = tk.StringVar()
file_path = tk.StringVar()
product = tk.StringVar()
output_path.set(desktop_path + os.sep + output_file_name)
window.title('企查查专用通讯录模板转换程序')

lable_product = tk.Label(window)
lable_product.place(x=80, y=90, height=23, width=66)
lable_product.configure(text='''产品标识:''')

txt_product = tk.Entry(window)
txt_product.place(x=160, y=90, height=27, width=284)
txt_product.configure(textvariable=product)

btn_file = tk.Button(window)
btn_file.place(x=70, y=140, height=28, width=69)
btn_file.configure(text='''转换文件''')
btn_file.configure(command=set_upload_file)

txt_file_path = tk.Entry(window)
txt_file_path.place(x=160, y=140, height=27, width=284)
txt_file_path.configure(textvariable=file_path)

btn_setpath = tk.Button(window)
btn_setpath.place(x=70, y=200, height=28, width=69)
btn_setpath.configure(text='''保存为''')
btn_setpath.configure(command=set_output_path)

txt_output_path = tk.Entry(window)
txt_output_path.place(x=160, y=200, height=27, width=284)
txt_output_path.configure(textvariable=output_path)


btn_output = tk.Button(window)
btn_output.place(x=190, y=290, height=38, width=109)
btn_output.configure(text='''导出转换表格''')
btn_output.configure(command=lambda : output_file(txt_file_path.get(), txt_product.get(), txt_output_path.get()))

statusbar = tk.Label(window, text="技术支持:山东中达物联科技有限公司 18678709906(同微信)", bd=1, relief=tk.SUNKEN, anchor=tk.W)
statusbar.pack(side=tk.BOTTOM, fill=tk.X)
'''
# 这部分是没有用 PAGE Gui实现的,第一次写,有很多不足
frm = tk.Frame(window)
frm.grid(padx='20', pady='30')
lable_product = tk.Label(frm, text="产品标识:")
lable_product.grid(row=0, column=0, ipadx='3', ipady='3', padx='10', pady='5')
txt_product = tk.Entry(frm, width='40', textvariable=product)
txt_product.grid(row=0, column=1)

btn_file = tk.Button(frm, text='转换文件', command=set_upload_file)
btn_file.grid(row=1, column=0, ipadx='3', ipady='3', padx='10', pady='20')
txt_file_path = tk.Entry(frm, width='40', textvariable=file_path)
txt_file_path.grid(row=1, column=1)

btn_setpath = tk.Button(frm, text='保存为', command=set_output_path)
btn_setpath.grid(row=2, column=0, ipadx='3', ipady='3', padx='10', pady='20')
txt_output_path = tk.Entry(frm, width='40', textvariable=output_path)
txt_output_path.grid(row=2, column=1)

btn_output = tk.Button(window, text="导出转换表格", command=lambda : output_file(txt_file_path.get(), txt_product.get(), txt_output_path.get()))
btn_output.grid(row=2, column=0)

status_bar_frame = tk.Frame(window, bg="#dfdfdf")
status_bar_frame.grid(row=3, column=0, columnspan=2, sticky="we")
status_bar = tk.Label(status_bar_frame, text="技术支持:济宁小墨匠智能科技有限公司 18678709906", bg="#dfdfdf")
status_bar.pack()
'''

# 窗口居中于屏幕
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
w = 520
h = 390
x = (screen_width - w) / 2
y = (screen_height - h) / 2

window.geometry("%dx%d+%d+%d" % (w, h, x, y))

window.mainloop()

# pyinstaller 打包:pyinstaller -i icon.ico -w -F main.py

项目总结

遇到的一些坑

python读写excel文件的库依赖问题

虽然只import了pandas,但是执行pd.read_excel的时候还是需要依赖xlrd模块,如果是xls文件还需要依赖openpyxl模块。不然会有报错。
在这里插入图片描述
在这里插入图片描述

打包的若干问题

问题1:win10下打包在win7下无法打开

原因分析:
很可能的原因是我打包的环境是Python3.9 64位,查了一下3.9是不支持win7的,还有就是64位机器打包的exe在32位机器上可能会打不开(没有测试)。
如果提示api-ms-win-core-path-l1-1-0.dll 丢失,十有八九是C++环境的问题,安装 Visual C++ Redistributable for Visual Studio 2015 就能解决。

在这里插入图片描述
如果出现下图的报错就是python3.9不支持win7造成的,在python3.8及以下虚拟环境下重新打包解决。
在这里插入图片描述

解决方案:
下载安装python3.8 32位,创建python3.8的虚拟环境,重新打包

问题2:pyinstaller打包太大

一定要在虚拟环境下打包!一定要在虚拟环境下打包!一定要在虚拟环境下打包!
开始打包的时候没有在虚拟环境下,打完包居然100多M,还运行报错,直接懵掉了。至于虚拟环境,pycharm自带的虚拟环境功能就挺好用。

扩展

资源下载

PAGE - Python Tkinter GUI 可视化生成软件

当前(截止到2021.03.28)最新版本 PAGE-6.0.1
http://page.sourceforge.net/#Download
访问国外网速慢点同学可以在这里下载
PAGE - Python Tkinter GUI 可视化生成软件 6.0.1

ActiveTcl

官方下载地址:https://www.activestate.com/products/tcl/downloads/ 官方下载需要登录,不想登录的同学在这里下载吧
ActiveTcl-8.6.9.8609.2-MSWin32-x64-5ccbd9ac8.zip

Tkinter 文档/教程

Graphical User Interfaces with Tk
Tkinter Tutorial
Python GUI编程(Tkinter)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值