Amazing-Python-Scripts

Форк
0
49 строк · 1.4 Кб
1
#!python3
2
# -*- coding: utf-8 -*-
3

4
import openpyxl
5
import sys
6

7
# inputs
8
print("This programme writes the data in any Comma-separated value file (such as: .csv or .data) to a Excel file.")
9
print("The input and output files must be in the same directory of the python file for the programme to work.\n")
10

11
csv_name = input("Name of the CSV file for input (with the extension): ")
12
sep = input("Separator of the CSV file: ")
13
excel_name = input("Name of the excel file for output (with the extension): ")
14
sheet_name = input("Name of the excel sheet for output: ")
15

16
# opening the files
17
try:
18
    wb = openpyxl.load_workbook(excel_name)
19
    sheet = wb.get_sheet_by_name(sheet_name)
20

21
    file = open(csv_name, "r", encoding="utf-8")
22
except:
23
    print("File Error!")
24
    sys.exit()
25

26
# rows and columns
27
row = 1
28
column = 1
29

30
# for each line in the file
31
for line in file:
32
    # remove the \n from the line and make it a list with the separator
33
    line = line[:-1]
34
    line = line.split(sep)
35

36
    # for each data in the line
37
    for data in line:
38
        # write the data to the cell
39
        sheet.cell(row, column).value = data
40
        # after each data column number increases by 1
41
        column += 1
42

43
    # to write the next line column number is set to 1 and row number is increased by 1
44
    column = 1
45
    row += 1
46

47
# saving the excel file and closing the csv file
48
wb.save(excel_name)
49
file.close()
50

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.