Skip to content

天楚锐齿

人工智能 云计算 大数据 物联网 IT 通信 嵌入式

天楚锐齿

  • 下载
  • 物联网
  • 云计算
  • 大数据
  • 人工智能
  • Linux&Android
  • 网络
  • 通信
  • 嵌入式
  • 杂七杂八

使用python从MySQL生成discuz的excel报表

2018-03-14
要求:每周从discuz的mysql数据库读取数据,生成所需要的xecel报表,模板格式如下:
同时生成统计图:

 

附,代码如下(需要根据上面的表格先做好excel模板):
#!/usr/bin/env python
# -*- coding: cp936 -*-
__author__ = ‘Max Shu’
import sys
import string
import os
import time
import datetime
import struct
import MySQLdb
import openpyxl
from openpyxl.styles import Style, PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.charts import LineChart, Reference, Series
from openpyxl.cell import coordinate_from_string, get_column_letter, get_column_interval, column_index_from_string
class BCDISCUZ_Statistics():
    conn = None
    usersTotal = 0
    usersActivityTotal = 0
    usersZombieTotal = 0
    postsMaxOfSingleUser = 0
    threadsMaxOfSingleUser = 0
    postsTotal = 0
    threadsTotal = 0
    postsMaxOfSingleThread = 0
    pvMaxOfSingleThread = 0
    forumsTotal =0
    postsMaxOfSingleForum = 0
    threadsMaxOfSingleForum = 0
    def __init__(self):
        try:
            self.conn = MySQLdb.connect(host = “localhost”,
                                    user = “root”,
                                    passwd = “xxxxx”,
                                    db = “discuz_ecloud”)
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            print “ERROR: Can’t connect to MySQL( localhost:3306 )”
            self.conn = None
            return
    def __del__(self):
        if self.conn != None:
            self.conn.commit()
            self.conn.close()
    def Print_All_Statistics_Value(self):
        print ” “
        print “usersTotal: %d” % self.usersTotal
        print “usersActivityTotal: %d” % self.usersActivityTotal
        print “usersZombieTotal: %d” % self.usersZombieTotal
        print “postsMaxOfSingleUser: %d” % self.postsMaxOfSingleUser
        print “threadsMaxOfSingleUser: %d” % self.threadsMaxOfSingleUser
        print ” “
        print “postsTotal: %d” % self.postsTotal
        print “threadsTotal: %d” % self.threadsTotal
        print “postsMaxOfSingleThread: %d” % self.postsMaxOfSingleThread
        print “pvMaxOfSingleThread: %d” % self.pvMaxOfSingleThread
        print ” “
        print “forumsTotal: %d” % self.forumsTotal
        print “postsMaxOfSingleForum: %d” % self.postsMaxOfSingleForum
        print “threadsMaxOfSingleForum: %d” % self.threadsMaxOfSingleForum
        print ” “
    def Get_All_Statistics_Value(self):
        self.Get_Users_Total()
        self.Get_Users_Activity_Total()
        self.Get_Users_Zombie_Total()
        self.Get_Posts_Max_Of_Single_User()
        self.Get_Threads_Max_Of_Single_User()
        self.Get_Posts_Total()
        self.Get_Threads_Total()
        self.Get_Posts_Max_Of_Single_Thread()
        self.Get_PV_Max_Of_Single_Thread()
        self.Get_Forums_Total()
        self.Get_Posts_Max_Of_Single_Forum()
        self.Get_Threads_Max_Of_Single_Forum()
    def Get_Users_Total(self):
        “””用户数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT COUNT(*) FROM pre_common_member;”)
            result_set = cursor.fetchall ()
#            for row in result_set:
#                print “%s” % (row[0])
#            print “Number of rows returned: %d” % cursor.rowcount
            cursor.close ()
            self.usersTotal = result_set[0][0]
            return self.usersTotal
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Users_Activity_Total(self):
        “””活跃用户数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT COUNT(*) FROM pre_common_member_status WHERE lastvisit >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 WEEK));”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.usersActivityTotal = result_set[0][0]
            return self.usersActivityTotal
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Users_Zombie_Total(self):
        “””僵尸用户数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT COUNT(*) FROM pre_common_member_status WHERE lastvisit <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 3 MONTH));”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.usersZombieTotal = result_set[0][0]
            return self.usersZombieTotal
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Posts_Max_Of_Single_User(self):
        “””单用户最多帖子数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT MAX(posts) FROM pre_common_member_count;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.postsMaxOfSingleUser = result_set[0][0]
            return self.postsMaxOfSingleUser
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Threads_Max_Of_Single_User(self):
        “””单用户最多主题数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT MAX(threads) FROM pre_common_member_count;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.threadsMaxOfSingleUser = result_set[0][0]
            return self.threadsMaxOfSingleUser
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Posts_Total(self):
        “””帖子数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT COUNT(*) FROM pre_forum_post;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.postsTotal = result_set[0][0]
            return self.postsTotal
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Threads_Total(self):
        “””主题数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT COUNT(*) FROM pre_forum_thread;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.threadsTotal = result_set[0][0]
            return self.threadsTotal
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Posts_Max_Of_Single_Thread(self):
        “””单主题最多帖子数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT MAX(t1.c1) FROM (SELECT COUNT(*) AS c1 FROM pre_forum_post GROUP BY tid) AS t1;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.postsMaxOfSingleThread = result_set[0][0]
            return self.postsMaxOfSingleThread
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_PV_Max_Of_Single_Thread(self):
        “””单主题最大PV值”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT MAX(views) FROM pre_forum_thread;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.pvMaxOfSingleThread = result_set[0][0]
            return self.pvMaxOfSingleThread
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Forums_Total(self):
        “””版块数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT COUNT(*) FROM pre_forum_forum WHERE STATUS=1 AND TYPE <> ‘group’;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.forumsTotal = result_set[0][0]
            return self.forumsTotal
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Posts_Max_Of_Single_Forum(self):
        “””单版块最多帖子数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT MAX(posts) FROM pre_forum_forum;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.postsMaxOfSingleForum = result_set[0][0]
            return self.postsMaxOfSingleForum
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Get_Threads_Max_Of_Single_Forum(self):
        “””单版块最多主题数”””
        if self.conn == None:
            return 0
        try:
            cursor = self.conn.cursor ()
            cursor.execute (“SELECT MAX(threads) FROM pre_forum_forum;”)
            result_set = cursor.fetchall ()
            cursor.close ()
            self.threadsMaxOfSingleForum = result_set[0][0]
            return self.threadsMaxOfSingleForum
        except MySQLdb.Error, e:
            print “Error %d: %s” % (e.args[0], e.args[1])
            return 0
    def Write_Excel_File(self):
        fileDir = u”Excel周报”
        fileNamePre = u”论坛现网环境业务统计周报-“
        dateStr = time.strftime(‘%Y%m%d’, time.localtime())
        # delete old file for today.
        fileName = os.path.join(fileDir, fileNamePre+dateStr+u”.xlsx”)
        fileName = fileName.encode(“cp936”)
        if os.path.isfile(fileName):
            os.remove(fileName)
        # get newest file for before.
        newestFileName = “”
        oldFileDate = “”
        fileDir = fileDir.encode(“cp936”)
        for file in os.listdir(fileDir):
            fileDate = file[len(fileNamePre):file.index(“.xlsx”)]
            if oldFileDate < fileDate:
                oldFileDate = fileDate
                newestFileName = file
        # open the newest file to operation.
        border=Border(left=Side(border_style=’thin’, color=’FF000000′),
                    right=Side(border_style=’thin’, color=’FF000000′),
                    top=Side(border_style=’thin’, color=’FF000000′),
                    bottom=Side(border_style=’thin’, color=’FF000000′))
        workBook = openpyxl.load_workbook(os.path.join(fileDir, newestFileName))
        sheet = workBook.get_sheet_by_name(u”Statistics”)
        row = sheet.max_row + 1
        # date
        sheet.cell(‘A’+str(row)).value = datetime.date.today()
        sheet.cell(‘A’+str(row)).number_format = “yyyy/mm/dd”
        sheet.cell(‘A’+str(row)).style = Style(border=border, number_format=’yyyy/mm/dd’)
        # user
        sheet.cell(‘B’+str(row)).value = self.usersTotal
        sheet.cell(‘B’+str(row)).style = Style(border=border)
        sheet.cell(‘C’+str(row)).value = self.usersActivityTotal
        sheet.cell(‘C’+str(row)).style = Style(border=border)
        sheet.cell(‘D’+str(row)).value = self.usersZombieTotal
        sheet.cell(‘D’+str(row)).style = Style(border=border)
        sheet.cell(‘E’+str(row)).value = self.postsMaxOfSingleUser
        sheet.cell(‘E’+str(row)).style = Style(border=border)
        sheet.cell(‘F’+str(row)).value = self.threadsMaxOfSingleUser
        sheet.cell(‘F’+str(row)).style = Style(border=border)
        # post
        sheet.cell(‘G’+str(row)).value = self.postsTotal
        sheet.cell(‘G’+str(row)).style = Style(border=border)
        sheet.cell(‘H’+str(row)).value = self.threadsTotal
        sheet.cell(‘H’+str(row)).style = Style(border=border)
        sheet.cell(‘I’+str(row)).value = self.postsMaxOfSingleThread
        sheet.cell(‘I’+str(row)).style = Style(border=border)
        sheet.cell(‘J’+str(row)).value = self.pvMaxOfSingleThread
        sheet.cell(‘J’+str(row)).style = Style(border=border)
        # forum
        sheet.cell(‘K’+str(row)).value = self.forumsTotal
        sheet.cell(‘K’+str(row)).style = Style(border=border)
        sheet.cell(‘L’+str(row)).value = self.postsMaxOfSingleForum
        sheet.cell(‘L’+str(row)).style = Style(border=border)
        sheet.cell(‘M’+str(row)).value = self.threadsMaxOfSingleForum
        sheet.cell(‘M’+str(row)).style = Style(border=border)
        # chart operation
        sheetChart = workBook.get_sheet_by_name(u”Chart”)
        chartTopPos = 5
        chartInterval = 5
        chartLeftPos = 10
        chartWidth = 1200
        realValueStartRow = 3
        labels = Reference(sheet, (realValueStartRow, column_index_from_string(‘A’)), (sheet.max_row, column_index_from_string(‘A’)))
        # No.1 chart
        values = Reference(sheet, (realValueStartRow, column_index_from_string(‘B’)), (sheet.max_row, column_index_from_string(‘B’)))
        series1 = Series(values, title=u”总用户数”, labels=labels)
        chartHeight = 300
        self.Execl_Insert_Chart(sheetChart, u”总用户数”, chartLeftPos, chartTopPos, chartWidth, chartHeight, series1)
        # No.2 chart
        values = Reference(sheet, (realValueStartRow, column_index_from_string(‘C’)), (sheet.max_row, column_index_from_string(‘C’)))
        series1 = Series(values, title=u”活跃用户数”, labels=labels)
        chartTopPos += (chartHeight + chartInterval)
        self.Execl_Insert_Chart(sheetChart, u”活跃用户数”, chartLeftPos, chartTopPos, chartWidth, chartHeight, series1)
        values = Reference(sheet, (realValueStartRow, column_index_from_string(‘G’)), (sheet.max_row, column_index_from_string(‘G’)))
        series1 = Series(values, title=u”总贴数”, labels=labels)
        chartTopPos += (chartHeight + chartInterval)
        self.Execl_Insert_Chart(sheetChart, u”总贴数”, chartLeftPos, chartTopPos, chartWidth, chartHeight, series1)
        values = Reference(sheet, (realValueStartRow, column_index_from_string(‘H’)), (sheet.max_row, column_index_from_string(‘H’)))
        series1 = Series(values, title=u”总主题数”, labels=labels)
        chartTopPos += (chartHeight + chartInterval)
        self.Execl_Insert_Chart(sheetChart, u”总主题数”, chartLeftPos, chartTopPos, chartWidth, chartHeight, series1)
        # save today file
        workBook.save(fileName)
    def Execl_Insert_Chart(self, sheet, title, left, top, width, height, *series):
        chart = LineChart()
        chart.title = title
        for serial in series:
            chart.append(serial)
        chart.drawing.left = left
        chart.drawing.top = top
        chart.drawing.width = width
        chart.drawing.height = height
        chart.margin_left = width – 100
        sheet.add_chart(chart)
if __name__ == ‘__main__’:
    bcdiscuz = BCDISCUZ_Statistics()
    if bcdiscuz.conn == None:
        exit(1)
    bcdiscuz.Get_All_Statistics_Value()
    bcdiscuz.Print_All_Statistics_Value()
    bcdiscuz.Write_Excel_File()
    bcdiscuz = None
    exit(0)
1,370次阅读

Post navigation

前一篇:

某云计算中心网络架构图

后一篇:

阿里云ECS主机和VPC限制

发表回复 取消回复

要发表评论,您必须先登录。

个人介绍

需要么,有事情这里找联系方式:关于天楚锐齿

=== 美女同欣赏,好酒共品尝 ===

微信扫描二维码赞赏该文章:

扫描二维码分享该文章:

分类

  • Linux&Android (81)
  • Uncategorized (1)
  • 下载 (28)
  • 云计算 (38)
  • 人工智能 (9)
  • 大数据 (35)
  • 嵌入式 (34)
  • 杂七杂八 (35)
  • 物联网 (65)
  • 网络 (25)
  • 通信 (22)

归档

近期文章

  • 飞书机器人发送卡片interactive消息
  • Springboot JPA实现对数据库表统一的增删改查
  • WEB的内容安全策略CSP(Content-Security-Policy)
  • CSS利用@media和viewport实现响应式布局自动适配手机电脑等
  • VUE前端增加国际化支持

近期评论

  • linux爱好者 发表在《Linux策略路由及iptables mangle、ip rule、ip route关系及一种Network is unreachable错误》
  • maxshu 发表在《使用Android的HIDL+AIDL方式编写从HAL层到APP层的程序》
  • Ambition 发表在《使用Android的HIDL+AIDL方式编写从HAL层到APP层的程序》
  • Ambition 发表在《使用Android的HIDL+AIDL方式编写从HAL层到APP层的程序》
  • maxshu 发表在《Android9下用ethernet 的Tether模式来做路由器功能》

阅读量

  • 使用Android的HIDL+AIDL方式编写从HAL层到APP层的程序 - 23,708次阅读
  • 卸载深信服Ingress、SecurityDesktop客户端 - 18,404次阅读
  • 车机技术之车规级Linux-Automotive Grade Linux(AGL) - 10,475次阅读
  • linux下的unbound DNS服务器设置详解 - 9,184次阅读
  • 在Android9下用ndk编译vSomeIP和CommonAPI以及使用例子 - 9,074次阅读
  • linux的tee命令导致ssh客户端下的shell卡住不动 - 8,579次阅读
  • Linux策略路由及iptables mangle、ip rule、ip route关系及一种Network is unreachable错误 - 8,069次阅读
  • 车机技术之360°全景影像(环视)系统 - 8,051次阅读
  • 车机技术之Android Automotive - 7,909次阅读
  • Windows下安装QEMU并在qemu上安装ubuntu和debian - 7,792次阅读

其他操作

  • 注册
  • 登录
  • 条目 feed
  • 评论 feed
  • WordPress.org

联系方式

地址
深圳市科技园

时间
周一至周五:  9:00~12:00,14:00~18:00
周六和周日:10:00~12:00

标签

android AT命令 CAN centos docker Hadoop hdfs ip java kickstart linux mapreduce mini6410 modem nova OAuth openstack os python socket ssh uboot 内核 协议 安装 嵌入式 性能 报表 授权 操作系统 数据 数据库 月报 模型 汽车 深信服 源代码 统计 编译 脚本 虚拟机 调制解调器 车机 金融 鉴权
© 2025 天楚锐齿