본문 바로가기
<개인공부> - IT/[Python]

Openpyxl - Make a report template

by Aggies '19 2020. 7. 30.
반응형

 

Let's create a report template as shown above.

import datetime
import openpyxl
from openpyxl.styles import Border, Side, Font, Alignment, PatternFill

def createExcelFile():
    
    # Excel File Creation
    nowDate = 'Report Date: ' + str(datetime.datetime.now().strftime('%Y-%m-%d'))
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = 'SNMP Community'
    
    # Pretty display for the File
    font = Font(bold=True)
    alignment = Alignment(horizontal='center')
    bgColor = PatternFill(fgColor='BFBFBFBF', patternType='solid')
    border = Border(left=Side(style='thin'),
                    right=Side(style='thin'),
                    top=Side(style='thin'),
                    bottom=Side(style='thin'))

    ws['A2'] = nowDate
    
    ws['A4'] = 'Hostname'
    ws['A4'].alignment = alignment
    ws['A4'].font = font
    ws['A4'].fill = bgColor
    ws['A4'].border = border

    ws['B4'] = 'IP Address'
    ws['B4'].alignment = alignment
    ws['B4'].font = font
    ws['B4'].fill = bgColor
    ws['B4'].border = border

    ws['C4'] = 'SNMP Community'
    ws['C4'].alignment = alignment
    ws['C4'].font = font  
    ws['C4'].fill = bgColor
    ws['C4'].border = border

    ws.column_dimensions['A'].width = 40
    ws.column_dimensions['B'].width = 15
    ws.column_dimensions['C'].width = 60
    
    fileName = 'SNMP_Community.xlsx'
    wb.save(fileName)
    wb.close()
반응형

'<개인공부> - IT > [Python]' 카테고리의 다른 글

Cisco ISE ERS API (Device Update)  (2) 2020.08.26
Basic usage exchangelib (MS Exchange)  (0) 2020.08.01
How to get a dict key in the list  (0) 2020.06.13
Python f-strings (Keep updating)  (0) 2020.04.04
List comprehension  (0) 2020.03.30