Write a simple Excel form with Python to get the Linux system information at the time

Source: Internet
Author: User

Recently in the study of the production of Excel table, by the way, by combining the previous learning content, using Python's two templates, is to obtain system information Psutil, and generate Excel table Xlsxwriter. Using these two templates will generate a simple Excel table that gets information about the Linux system at that time, such as memory status, disk status, network card traffic, and so on.

First of all, Psutil and Xlsxwriter are to be installed.
Installing Psutil

wget --no-check-certificae  https://pypi.python.org/packages/e2/e1/600326635f97fee89bf8426fef14c5c29f4849c79f68fd79f433d8c1bd96/psutil-5.4.3.tar.gztar -xf psutil-5.4.3.tar.gzcd psutil-5.4.3python setup.py install

Installing Xlsxwriter

pip install XlsxWriter

Then start writing the script

#!/usr/bin/python#-*-coding:utf-8-*-import psutilimport xlsxwriterfrom xlsxwriter import Workbookimport time## Cpuuser_cpu_time = Psutil.cpu_times (). User # #获取用户时间比cpu_number = psutil.cpu_count (logical=false) # #CPU物理个数 # #内存信息mem_ Total = Psutil.virtual_memory (). Total/1048576mem_free = Psutil.virtual_memory (). free/1048576mem_used = Psutil.virtual_memory (). used/1048576# #磁盘信息disk_total = psutil.disk_usage ('/'). total/1048576disk_used = Psutil.disk _usage ('/'). Used/1048576disk_free = Psutil.disk_usage ('/'). free/1048576# #网络信息net_in = Psutil.net_io_counters (). Bytes_recv/1048576net_out = Psutil.net_io_counters (). bytes_sent/1048576# #当前时间time = Time.strftime ('%Y-%m-%d%H:%M:% S ', Time.localtime (Time.time ()) # #建立一个列表存放获取的系统信息text = [time,user_cpu_time,cpu_number,mem_total,mem_free,mem_ Used,disk_total,disk_used,disk_free,net_in,net_out]workbook = Xlsxwriter. Workbook (' status.xlsx ') # #建立一个excel表格 # #建立一个工作表对象, which is the sheet1,sheet2 in the lower left corner of Excel, is built here. Worksheet = Workbook.add_worksheet () # #存放excel表格标题信息的List title = [u ' time ', U ' user CPU time ratio ', U ' CPU amount ', U ' total memory ', U ' used memory ', U ' free memory ', U ' disk total ', U ' used disk ', U ' free disk ', U ' nic out ', U ' nic in ']## Set the contents of the cell format, such as Set_border is the border bold, Set_bg_color is the cell background color format_title = Workbook.add_format () format_title.set_border (1) Format_title.set_bg_color (' #cccccc ') Format_text = Workbook.add_format () format_text.set_border (1) # # Write cell Operation Worksheet.set_column (' A:k ', #) # #设置A到K列宽度20像素worksheet. Write_row (' A1 ', Title,format_title) # # The title list has a A1 beginning to be written horizontally, and the format is Format_titleworksheet.write_row (' A2 ', Text,format_text) workbook.close () # #记得将工作表关闭

Then execute the script to get a table file, open the table file to see

That's OK.

Write a simple Excel form with Python to get the Linux system information at the time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.