python批量获取手机归属地,手机号归属地批量查询

前一阵因业务需要,写了一个小工具,现在把代码分享出来:

本人python版本

 

 pycharm版本 PyCharm 2019.3.3 x64

如下是代码

import os
import xlrd
import xlwt
from phone import Phone
from tkinter import filedialog, messagebox, Tk, Entry, Label, StringVar, Button


interface = Tk()
interface.title('手机号归属地查询')
interface.geometry('400x100')


def save_new_xls(list):
    workbook = xlwt.Workbook();
    worksheet = workbook.add_sheet('工作表1')
    #print(list)
    for i in range(len(list)):
        try:
            #print(type(list[i]))
            worksheet.write(i, 0, list[i]['phone'])
            worksheet.write(i, 1, list[i]['province'])
            worksheet.write(i, 2, list[i]['city'])
            worksheet.write(i, 3, list[i]['zip_code'])
            worksheet.write(i, 4, list[i]['area_code'])
            worksheet.write(i, 5, list[i]['phone_type'])
        except:
            continue
    messagebox.showinfo('提示', '数据处理已完成,请选择要存储的目录与文件名!')
    filename = filedialog.asksaveasfilename(filetypes=[('xlsx', '*.xlsx')], initialdir='D:\\')
    if filename:
        filename = filename + '.xlsx'
        workbook.save(filename)
        if messagebox.askokcancel('提示', '保存成功!现在要打开这个文件吗?'):
            os.startfile(filename)


def read_excel_b(fpath):
    global headerstart
    global phoneincolumn
    xl = xlrd.open_workbook(fpath)
    tb = xl.sheet_by_index(0)
    rows = tb.nrows
    list = []
    for row in range(int(headerstart.get()), rows):
        p = tb.cell(row,int(phoneincolumn.get())).value
        try:
            info = Phone().find(int(p))
        except:
            info = {}
        list.append(info)
    path = os.path.dirname(os.path.abspath(fpath))
    #print(path)
    #print(list)
    save_new_xls(list)


def open_file():
    a = filedialog.askopenfilename()
    if a:
        read_excel_b(a)
    return a

addr = StringVar(value='0')
addr2 = StringVar(value='0')
headerstart_text = Label(interface, text="数据起始行(第一行为0):")
headerstart = Entry(interface, textvariable=addr)
headerstart_text.grid(column=2, row=2)
headerstart.grid(column=3, row=2)

phoneincolumn_text = Label(interface, text="电话号码所在列(第A列为0):")
phoneincolumn = Entry(interface, textvariable=addr2)
phoneincolumn_text.grid(column=2, row=3)
phoneincolumn.grid(column=3, row=3)

label = Label(interface, text='打开源数据')
label.grid(column=2, row=4)
button = Button(interface, text='选择文件', command=open_file)
button.grid(column=3, row=4)

copyrights = Label(interface, text="by:Blver", fg="red")
copyrights.grid(column=4, row=5)

interface.mainloop()

因引入了tkinter,所以这个小工具是有图形界面的 (笑)

逻辑就是打开文件,然后用xlrd读取,再用phone模块查出号码信息,再用xlwt写入新xls,最后保存。

需要处理的模板如果是这样

 

 

那么如下的数据起始行就是0

 

 处理后的信息会在B以后的列

 

posted @ 2021-04-10 13:41  红岸  阅读(861)  评论(0编辑  收藏  举报