- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvExcel.py
More file actions
Latest commit
61 lines (49 loc) · 1.86 KB
/
Copy pathcsvExcel.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
#
importtime, socket
importpandasaspd
frompandas.io.excelimportExcelWriter
frompathlibimportPath
fromopenpyxlimportload_workbook
fromstringimportascii_uppercase
today=time.strftime("%Y%m%d")
csv_path=Path('.')
defget_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",
}
returnenv.get(fqdn)
defcreate_report(dir1):
'''Look for csv files in dir and create a list of filenames'''
csv_files_list= [file.stemforfileincsv_path.glob('*.csv')]
'''Create new workbook and add each csv in own sheet'''
withExcelWriter('temp.xlsx') asew:
forcsv_fileincsv_files_list:
pd.read_csv(csv_file+'.csv').to_excel(ew, sheet_name=csv_file, index=False)
defformat_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'''
foriinrange(len(wb.worksheets)):
wb.active=i
'''For each worksheet set the col width for A,B,C, leave the others at default'''
forcolumninascii_uppercase:
if (column=='A'orcolumn=='B'orcolumn=='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')