Python 讀取Excel文件並把相關信息@給相應的微信好友

工作中每日需要把Excel文件整理插入透視表,對文件信息進行整理,並把相對應每條信息的負責人發送給微信羣裏相對應的微信好友,最後對Excel文件進行統計,發送每個負責人的統計信息。

Python 通過讀取Excel文件並把相關信息@給相應的微信好友

採用wxpy第三方包(微信模塊登入微信),通過微信二維碼掃描進入。

# coding=utf-8
from Tkinter import *
from tkFileDialog import *
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Border, Side, Font  # 設置字體和邊框需要的模塊
import xlrd #讀excel要用到xlrd模塊
import xlwt #寫excel表要用到xlwt模塊
from datetime import datetime
from xlrd import xldate_as_tuple
from wxpy import *

class App:
    def __init__(self, root):
        frame = Frame(root)  # container
        frame.pack()

        self.button1 = Button(frame, text="退服日報文件", command=self.filefound1, width=20, height=1).grid(row=0, column=0)
        # self.button1.pack()
        self.button2 = Button(frame, text="運行", command=self.execuate, width=20, height=1).grid(row=2, column=0)
        # self.button3.pack()
        self.button3 = Button(frame, text="退出", command=frame.quit, width=20, height=1).grid(row=3, column=0)
        # self.button4.pack()

        self.e = Entry(frame)
        self.e.grid(row=0, column=2)
        self.e.delete(0, END)  # 將輸入框裏面的內容清空
        self.e.insert(0, '顯示文件路徑')

        self.filepath1 = StringVar()

    def filefound1(self):
        self.filepath1 = askopenfilename()
        print self.filepath1
        self.e.delete(0, END)  # 將輸入框裏面的內容清空
        self.e.insert(0, self.filepath1)
        return self.filepath1

    def execuate(self):
        print self.filepath1
        wb_yesterday = xlrd.open_workbook(self.filepath1)
        site_details_sheet = wb_yesterday.sheet_by_name(u'詳細', )  # 所有清單
        site_details_dict = {u'杜x': [], u'林x': [], u'唐x': [], u'鄭x': [], u'馬x': [], u'湯x': [],
                            u'邵x': [], u'胡x': [], u'吳x': [], u'陳x': [], u'鄔x': []}
        for i in range(1, site_details_sheet.nrows):  # 建立基站與維護經理的字典對應關係
           # print site_details_sheet.cell(i, 1).value, site_details_sheet.cell(i, 2).value, site_details_sheet.cell(i, 3).value, site_details_sheet.cell(i, 4).value
            if site_details_dict.has_key(site_details_sheet.cell(i, 3).value):
                site_details_dict[site_details_sheet.cell(i, 3).value].append(i)

        Statistics_dict = {u'杜x': [0, 0.0], u'林x': [0, 0.0], u'唐x': [0, 0.0], u'鄭x': [0, 0.0], u'馬x': [0, 0.0],
                            u'湯x': [0, 0.0], u'邵x': [0, 0.0], u'胡x': [0, 0.0], u'吳x': [0, 0.0], u'陳x': [0, 0.0],
                            u'鄔x': [0, 0.0], }
        for key, value in site_details_dict.items():
            if len(value) < 1:
                continue
            else:
                massage = u'@%s\n去激活時間, 站名,故障原因, 開銷天\n' % key
                Statistics_dict[key][0] = len(value)
                for num in value:
                    massage += site_details_sheet.cell(num, 0).value + u',   ' + site_details_sheet.cell(num,1).value + u',   ' + site_details_sheet.cell(num, 2).value + u',    ' + str(site_details_sheet.cell(num, 4).value)
                    massage += u'\n'
                Statistics_dict[key][1] += site_details_sheet.cell(num, 4).value
            group.send(massage)
            print massage
        Statistics_massage = u'\n\n負責人,基站數,開銷天\n'
        # sorted()都接受一個參數reverse(True or False)來表示升序或降序排序。
        Statistics_Rank = sorted(Statistics_dict.items(), key=lambda item: item[1], reverse=True)
        # print Statistics_Rank
        i = 1
        for item in Statistics_Rank:
            if item[1][0] > 0:
                Statistics_massage += item[0] + u',  ' + str(item[1][0]) + u',  ' + str(item[1][1]) + u'\n'
        group.send(Statistics_massage)
        print Statistics_massage

if __name__ == '__main__':
    root = Tk()
    root.title('微信發送基站故障信息')
    app = App(root)
    root.mainloop()

程序執行結果(對執行結果進行部分隱藏):

D:\Python27\python.exe C:/Users/kai/PycharmProjects/Office/Wechat_Group_Without_Service_GUI.py
C:/Users/kai/Desktop/站點激活20190306170454.XLS
C:/Users/kai/Desktop/站點激活20190306170454.XLS
去激活時間 站名 故障原因 開銷天
@邵x
去激活時間, 站名,故障原因, 開銷天
2019-01-16 17:36:41,   x,   x,    48.9

@馬x
去激活時間, 站名,故障原因, 開銷天
2019-03-04 06:53:37,   x,   x,    2.42
xxxxx

@林x
去激活時間, 站名,故障原因, 開銷天
2019-03-01 13:48:57,   x,   x,    5.13
xxxxxx

@湯x
去激活時間, 站名,故障原因, 開銷天
2019-02-27 07:21:42,   x,   x,    7.4
xxxxxxxxx

@鄭x
去激活時間, 站名,故障原因, 開銷天
2019-03-05 16:43:47,   x,   x,    1.01
xxxxxx

@唐x
去激活時間, 站名,故障原因, 開銷天
2019-03-06 11:31:32,   x,   x,    0.22
xxxxxx



負責人,基站數,開銷天
唐x,  21,  52.1
馬x,  11,  55.3
湯x,  8,  63.1
林x,  3,  48.2
鄭x,  3,  1.06
邵x,  1,  48.9


Process finished with exit code 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章