-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_excel.py
More file actions
36 lines (28 loc) · 971 Bytes
/
csv_to_excel.py
File metadata and controls
36 lines (28 loc) · 971 Bytes
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
import os
import sys
import pandas as pd
import xlsxwriter
#check if the file is correct and exisits
fn = sys.argv[1]
if os.path.exists(fn) and fn[-3:]=='csv':
print(os.path.basename(fn))
else:
print("File not found")
exit()
# Load the CSV file into a DataFrame
df = pd.read_csv(fn)
#Convert csv to excel
writer = pd.ExcelWriter(fn[:-4]+'.xlsx', engine='xlsxwriter')
df.to_excel(
writer, sheet_name='Testing', startrow=1, startcol=1, )
#Load the worksheet object
worksheet = writer.sheets['Testing']
# Get the dimensions of the dataframe.
(max_row, max_col) = df.shape
# Create a list of column headers, to use in add_table().
column_settings = [{'header': column} for column in ['#'] + df.columns.tolist()]
# Add the Excel table structure. Pandas will add the data.
worksheet.add_table(1, 1, max_row+1, max_col+1,
{'columns': column_settings,
'style': 'Table Style Light 18'})
writer.close()