From 9b343ded914b5a60c138232ae15f009bd07da8d6 Mon Sep 17 00:00:00 2001 From: Martinxux Date: Fri, 16 Aug 2024 13:33:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=8E=A8Refactor:=20Remove=20redundant?= =?UTF-8?q?=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSV to Excel/csv_excel.py | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/CSV to Excel/csv_excel.py b/CSV to Excel/csv_excel.py index 81066393..67cc9aab 100644 --- a/CSV to Excel/csv_excel.py +++ b/CSV to Excel/csv_excel.py @@ -32,37 +32,4 @@ workbook.save(ename) -file.close() - -csv_name = input("Name of the input CSV file with extension: ") -sep = input("Separator of the CSV file: ") -ename = input("Name of the output excel file with extension: ") -sname = input("Name of the output excel sheet: ") -try: - workbook = openpyxl.load_workbook(ename) - sheet = workbook.get_sheet_by_name(sname) - - file = open(csv_name, "r", encoding="utf-8") -except: - print("Error: File not found") - sys.exit() -excel_row = 1 -excel_column = 1 - -for lines in file: - - lines = lines[:-1] - lines = lines.split(sep) - - for dat in lines: - - sheet.cell(excel_row, excel_column).value = dat - - excel_column += 1 - - excel_column = 1 - excel_row += 1 - - -workbook.save(ename) -file.close() +file.close() \ No newline at end of file From ccb78dc98fd5f18c516626818ab445801efe3d91 Mon Sep 17 00:00:00 2001 From: Martinxux Date: Fri, 16 Aug 2024 13:37:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9BFix:=20The=20get=5Fsheet=5Fby?= =?UTF-8?q?=5Fname=20method=20in=20the=20class=20Workbook=20has=20been=20d?= =?UTF-8?q?eprecated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSV to Excel/csv_excel.py | 62 ++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/CSV to Excel/csv_excel.py b/CSV to Excel/csv_excel.py index 67cc9aab..19fa8e11 100644 --- a/CSV to Excel/csv_excel.py +++ b/CSV to Excel/csv_excel.py @@ -1,35 +1,37 @@ import openpyxl -import sys +import os +# Get the CSV and Excel file names from the user csv_name = input("Name of the input CSV file with extension: ") sep = input("Separator of the CSV file: ") -ename = input("Name of the output excel file with extension: ") -sname = input("Name of the output excel sheet: ") +excel_name = input("Name of the output excel file with extension: ") +sheet_name = input("Name of the output excel sheet: ") + +# Load the CSV file +if os.path.exists(excel_name): + workbook = openpyxl.load_workbook(excel_name) + sheet = workbook[sheet_name] if sheet_name in workbook.sheetnames else workbook.create_sheet(sheet_name) +else: + workbook = openpyxl.Workbook() + sheet = workbook.active + sheet.title = sheet_name + +# Write the CSV data to the Excel sheet try: - workbook = openpyxl.load_workbook(ename) - sheet = workbook.get_sheet_by_name(sname) - - file = open(csv_name, "r", encoding="utf-8") -except: - print("Error: File not found") - sys.exit() -excel_row = 1 -excel_column = 1 - -for lines in file: - - lines = lines[:-1] - lines = lines.split(sep) - - for dat in lines: - - sheet.cell(excel_row, excel_column).value = dat - - excel_column += 1 - - excel_column = 1 - excel_row += 1 - - -workbook.save(ename) -file.close() \ No newline at end of file + with open(csv_name, "r", encoding="utf-8") as file: + excel_row = 1 + for line in file: + data = line.strip().split(sep) + excel_column = 1 + for value in data: + sheet.cell(row=excel_row, column=excel_column, value=value) + excel_column += 1 + excel_row += 1 + + # Save the Excel file + workbook.save(excel_name) + +except FileNotFoundError: + print("Error: The CSV file was not found.") +except Exception as e: + print(f"An error occurred: {e}") \ No newline at end of file