forked from wasmerio/Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_excel.py
More file actions
62 lines (50 loc) · 2.43 KB
/
Copy pathcsv_excel.py
File metadata and controls
62 lines (50 loc) · 2.43 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
61
import openpyxl
import os
from openpyxl.styles import Font, PatternFill
# Get the CSV and Excel file names from the user
csv_files = input("Enter the CSV files separated by commas (e.g., file1.csv, file2.csv): ").split(',')
sep = input("Separator of the CSV files (default is ','): ") or ',' # Default to comma separator if not provided
excel_name = input("Name of the output Excel file with extension: ")
# Load or create Excel workbook
if os.path.exists(excel_name):
workbook = openpyxl.load_workbook(excel_name)
else:
workbook = openpyxl.Workbook()
# Loop over multiple CSV files to write them into different sheets
for csv_name in csv_files:
csv_name = csv_name.strip() # Trim any whitespace
sheet_name = os.path.splitext(os.path.basename(csv_name))[0] # Sheet name based on the CSV filename
# Create a new sheet for each CSV file
if sheet_name in workbook.sheetnames:
sheet = workbook[sheet_name]
else:
sheet = workbook.create_sheet(sheet_name)
# Write CSV data to the Excel sheet
try:
with open(csv_name, "r", encoding="utf-8") as file:
excel_row = 1
header_detected = False # Flag to check if header formatting should be applied
for line in file:
data = line.strip().split(sep)
excel_column = 1
# Apply header formatting for the first row (headers)
if not header_detected:
for value in data:
cell = sheet.cell(row=excel_row, column=excel_column, value=value)
# Apply bold font and background color for the header row
cell.font = Font(bold=True)
cell.fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
excel_column += 1
header_detected = True # Mark the first row as header
else:
for value in data:
sheet.cell(row=excel_row, column=excel_column, value=value)
excel_column += 1
excel_row += 1
except FileNotFoundError:
print(f"Error: The CSV file '{csv_name}' was not found.")
except Exception as e:
print(f"An error occurred while processing {csv_name}: {e}")
# Save the Excel file with all sheets
workbook.save(excel_name)
print(f"All CSV files have been processed and saved to {excel_name}.")