Amazing-Python-Scripts
49 строк · 1.4 Кб
1#!python3
2# -*- coding: utf-8 -*-
3
4import openpyxl5import sys6
7# inputs
8print("This programme writes the data in any Comma-separated value file (such as: .csv or .data) to a Excel file.")9print("The input and output files must be in the same directory of the python file for the programme to work.\n")10
11csv_name = input("Name of the CSV file for input (with the extension): ")12sep = input("Separator of the CSV file: ")13excel_name = input("Name of the excel file for output (with the extension): ")14sheet_name = input("Name of the excel sheet for output: ")15
16# opening the files
17try:18wb = openpyxl.load_workbook(excel_name)19sheet = wb.get_sheet_by_name(sheet_name)20
21file = open(csv_name, "r", encoding="utf-8")22except:23print("File Error!")24sys.exit()25
26# rows and columns
27row = 128column = 129
30# for each line in the file
31for line in file:32# remove the \n from the line and make it a list with the separator33line = line[:-1]34line = line.split(sep)35
36# for each data in the line37for data in line:38# write the data to the cell39sheet.cell(row, column).value = data40# after each data column number increases by 141column += 142
43# to write the next line column number is set to 1 and row number is increased by 144column = 145row += 146
47# saving the excel file and closing the csv file
48wb.save(excel_name)49file.close()50