-
-
Notifications
You must be signed in to change notification settings - Fork 387
Optimize the py script CSV to Excel #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 32 additions & 63 deletions
CSV to Excel/csv_excel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +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() | ||
|
|
||
| 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() | ||
| 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}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.