-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvExcel.py
More file actions
61 lines (49 loc) · 1.86 KB
/
csvExcel.py
File metadata and controls
61 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Convert csv files into Excel workbook
# Format col widths to fixed width
#
import time, socket
import pandas as pd
from pandas.io.excel import ExcelWriter
from pathlib import Path
from openpyxl import load_workbook
from string import ascii_uppercase
today = time.strftime("%Y%m%d")
csv_path = Path('.')
def get_host():
'''Get the hostname'''
fqdn = socket.gethostname()
'''Create dict with hostnames to friendly names'''
env = {
"devA-001.example.com" : "devA",
"prodA-001.example.com" : "prodA",
"prodB-001.example.com" : "prodB",
"WorkLaptop" : "laptop",
"ThinkPad" : "thinkpad",
}
return env.get(fqdn)
def create_report(dir1):
'''Look for csv files in dir and create a list of filenames'''
csv_files_list = [file.stem for file in csv_path.glob('*.csv')]
'''Create new workbook and add each csv in own sheet'''
with ExcelWriter('temp.xlsx') as ew:
for csv_file in csv_files_list:
pd.read_csv(csv_file + '.csv').to_excel(ew, sheet_name=csv_file, index=False)
def format_width(wbook):
'''Load workbook created from create_report func'''
wb = load_workbook(wbook)
'''Interate over total nmuber of sheets and adjust column widths
Work out the total number of worksheets, and set each one active'''
for i in range(len(wb.worksheets)):
wb.active = i
'''For each worksheet set the col width for A,B,C, leave the others at default'''
for column in ascii_uppercase:
if (column=='A' or column=='B' or column=='C'):
wb.active.column_dimensions[column].width = 50
else:
wb.active.column_dimensions[column].width = 8
'''Save final workbook'''
wb.save(f'{get_host()}_rpm_excel_{today}.xlsx')
'''Call the functions'''
create_report(csv_path)
time.sleep(1)
format_width('temp.xlsx')