1
1
import openpyxl
2
2
import os
3
+ from openpyxl .styles import Font , PatternFill
3
4
4
5
# Get the CSV and Excel file names from the user
5
- csv_name = input ("Name of the input CSV file with extension: " )
6
- sep = input ("Separator of the CSV file: " )
7
- excel_name = input ("Name of the output excel file with extension: " )
8
- sheet_name = input ("Name of the output excel sheet: " )
6
+ csv_files = input ("Enter the CSV files separated by commas (e.g., file1.csv, file2.csv): " ).split (',' )
7
+ sep = input ("Separator of the CSV files (default is ','): " ) or ',' # Default to comma separator if not provided
8
+ excel_name = input ("Name of the output Excel file with extension: " )
9
9
10
- # Load the CSV file
10
+ # Load or create Excel workbook
11
11
if os .path .exists (excel_name ):
12
12
workbook = openpyxl .load_workbook (excel_name )
13
- sheet = workbook [sheet_name ] if sheet_name in workbook .sheetnames else workbook .create_sheet (sheet_name )
14
13
else :
15
14
workbook = openpyxl .Workbook ()
16
- sheet = workbook .active
17
- sheet .title = sheet_name
18
-
19
- # Write the CSV data to the Excel sheet
20
- try :
21
- with open (csv_name , "r" , encoding = "utf-8" ) as file :
22
- excel_row = 1
23
- for line in file :
24
- data = line .strip ().split (sep )
25
- excel_column = 1
26
- for value in data :
27
- sheet .cell (row = excel_row , column = excel_column , value = value )
28
- excel_column += 1
29
- excel_row += 1
30
-
31
- # Save the Excel file
32
- workbook .save (excel_name )
33
-
34
- except FileNotFoundError :
35
- print ("Error: The CSV file was not found." )
36
- except Exception as e :
37
- print (f"An error occurred: { e } " )
15
+
16
+ # Loop over multiple CSV files to write them into different sheets
17
+ for csv_name in csv_files :
18
+ csv_name = csv_name .strip () # Trim any whitespace
19
+ sheet_name = os .path .splitext (os .path .basename (csv_name ))[0 ] # Sheet name based on the CSV filename
20
+
21
+ # Create a new sheet for each CSV file
22
+ if sheet_name in workbook .sheetnames :
23
+ sheet = workbook [sheet_name ]
24
+ else :
25
+ sheet = workbook .create_sheet (sheet_name )
26
+
27
+ # Write CSV data to the Excel sheet
28
+ try :
29
+ with open (csv_name , "r" , encoding = "utf-8" ) as file :
30
+ excel_row = 1
31
+ header_detected = False # Flag to check if header formatting should be applied
32
+
33
+ for line in file :
34
+ data = line .strip ().split (sep )
35
+ excel_column = 1
36
+
37
+ # Apply header formatting for the first row (headers)
38
+ if not header_detected :
39
+ for value in data :
40
+ cell = sheet .cell (row = excel_row , column = excel_column , value = value )
41
+ # Apply bold font and background color for the header row
42
+ cell .font = Font (bold = True )
43
+ cell .fill = PatternFill (start_color = "FFFF00" , end_color = "FFFF00" , fill_type = "solid" )
44
+ excel_column += 1
45
+ header_detected = True # Mark the first row as header
46
+ else :
47
+ for value in data :
48
+ sheet .cell (row = excel_row , column = excel_column , value = value )
49
+ excel_column += 1
50
+
51
+ excel_row += 1
52
+
53
+ except FileNotFoundError :
54
+ print (f"Error: The CSV file '{ csv_name } ' was not found." )
55
+ except Exception as e :
56
+ print (f"An error occurred while processing { csv_name } : { e } " )
57
+
58
+ # Save the Excel file with all sheets
59
+ workbook .save (excel_name )
60
+
61
+ print (f"All CSV files have been processed and saved to { excel_name } ." )
62
+
0 commit comments