Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
DhanushNehru merged 2 commits into DhanushNehru:master from Martinxux:master
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 32 additions & 63 deletions CSV to Excel/csv_excel.py
View file Open in desktop
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}")

AltStyle によって変換されたページ (->オリジナル) /