I tried to output the rpm list of SSH login destination to an Excel sheet with Python + openpyxl.

What is openpyxl

openpyxl is a library for reading and writing * .xlsx in Python.

A Python library to read/write Excel 2010 xlsx/xlsm files https://openpyxl.readthedocs.org/en/default/

It's not very interesting to use it alone, but some people may be happy to be able to pick up the configuration information of the SSH login destination and automatically generate the parameter sheet of Excel, so I tried it a little.

Try to write the code

Here is the sample code that was created by trying out the functions that could be used one by one.

createlPackageList.py


import ssh
from openpyxl import Workbook
from openpyxl.styles.borders import Side, Border
from openpyxl.styles import Style, PatternFill

hostname = 'foo.bar.com'
username = 'root'
password = 'XXXXXXXX'
port = 22

client = ssh.SSHClient()
client.set_missing_host_key_policy(ssh.AutoAddPolicy())
client.connect(hostname, username = username, password = password, port = port)
stdin, stdout, stderr = client.exec_command('rpm -qa --queryformat="%{NAME},%{VERSION},%{RELEASE},%{ARCH},%{GROUP}\n" | sort')

wb = Workbook()
ws = wb.active

ws.append(['Name', 'Version', 'Release', 'Architecture', 'Group'])

for line in stdout.read().split('\n'):
    ws.append(list(line.split(',')))

thin_border = Border(left = Side(style = 'thin'), 
                     right = Side(style = 'thin'), 
                     top = Side(style = 'thin'), 
                     bottom = Side(style = 'thin'))
 
my_style = Style(border = thin_border)

for _row in ws.iter_rows('A1:E' + str(ws.max_row)):
    for _cell in _row:
        _cell.style = my_style

lightblueFill = PatternFill(start_color = '0000FFFF',
                      end_color = '0000FFFF',
                      fill_type = 'solid')

for _row in ws.iter_rows('A1:E1'):
    for _cell in _row:
        _cell.fill = lightblueFill

ws.column_dimensions['A'].width = 45
ws.column_dimensions['B'].width = 15
ws.column_dimensions['C'].width = 28
ws.column_dimensions['E'].width = 30

wb.save("output.xlsx")

Since openpyxl internally treats Excel rows as a list, here, the rpm -qa… | sort execution result (stdout) executed at the SSH login destination is read line by line, and the column-by-column split is added to the list. You can convert it and append it to the Workbook class worksheet ws.

You can also border cells, specify colors, or change the width of columns in the Styles module.

Generated Excel file

The following Excel file is created by executing the above script. Of course, no artificial editing work is done after the file is generated.

openpyxlの画面出力例_20151202_002.png

If you have a library that is easy to use in Python, it's very convenient because you can easily do all this with dozens of lines of code.

Environment: Confirmed with Python 2.7.9 + openpyxl 2.3.1.

Recommended Posts

I tried to output the rpm list of SSH login destination to an Excel sheet with Python + openpyxl.
I want to output the beginning of the next month with Python
I tried to improve the efficiency of daily work with Python
I tried to get the authentication code of Qiita API with Python.
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried to output LLVM IR with Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to implement an artificial perceptron with python
I tried to solve the problem with Python Vol.1
I tried to summarize the string operations of Python
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to put out the frequent word ranking of LINE talk with Python
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to find out how to streamline the work flow with Excel x Python ①
I tried to find out how to streamline the work flow with Excel x Python ③
I tried to compare the processing speed with dplyr of R and pandas of Python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to find the average of the sequence with TensorFlow
[Python] I tried to visualize the follow relationship of Twitter
Debug by attaching to the Python process of the SSH destination
I tried to divide the file into folders with Python
How to write offline real time I tried to solve the problem of F02 with Python
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to solve the ant book beginner's edition with python
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
Output the contents of ~ .xlsx in the folder to HTML with Python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to expand the size of the logical volume with LVM
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to make an image similarity function with Python + OpenCV
PhytoMine-I tried to get the genetic information of plants with Python
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
I want to extract an arbitrary URL from the character string of the html source with python
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
[Python] I tried to visualize the prize money of "ONE PIECE" over 100 million characters with matplotlib.
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
I tried sending an email with python.
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I tried to automatically extract the movements of PES players with software
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I want to color a part of an Excel string in Python
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to verify and analyze the acceleration of Python by Cython
I tried to analyze the negativeness of Nono Morikubo. [Compare with Posipa]
I tried to visualize the text of the novel "Weathering with You" with WordCloud
I measured the speed of list comprehension, for and while with python2.7.