#!python3
# -*- coding: utf-8 -*-
from gooey import Gooey, GooeyParser
from message import display_message
import os, sys, pathlib
from openpyxl import load_workbook
@Gooey(dump_build_config=True,
program_name="The Totally Awesome...COSMGC Foursomes Count Importer",
default_size=(600,400))
def parse_args():
prog_descrip = 'Moving csv data to an Excel workbook. The mighty Kipster!'
parser = GooeyParser(description=prog_descrip)
# parser = GooeyParser()
parser.add_argument('csv_source_file',
metavar='Name/location of csv file:',
action='store',
widget='FileChooser',
help="csv file with the data to import into Excel")
parser.add_argument('xcl_output_file',
metavar='Name/location of target Excel file:',
action='store',
widget='FileChooser',
help="excel file to receive csv data",
default=r"\\DS218Plus\home\Drive\General\2024 GG Foursomes Count.xlsx")
args = parser.parse_args()
return args
def main():
args = parse_args()
csv_name = args.csv_source_file
excel_name = args.xcl_output_file
# dnldpath = r'c:\users\kip\Downloads'
# excel_name = r'\\DS218Plus\home\Drive\General\2024 GG Foursomes Count.xlsx'
print()
print("You must select a csv file that contains the data you want to copy. You selected "+csv_name+".")
print()
print("This program writes the data from the comma-separated value file (such as: .csv, .txt or .data) to the Excel file you selected, " + excel_name)
print()
sep = ","
excelFile = excel_name
try:
wb = load_workbook(filename=excel_name)
wb.save(excel_name)
except Exception as e:
print("...............E R R O R ................." + str(e))
print()
print("The selected Excel file is missing or open. If it is open, please close: " + excelFile + " then click 'Restart' to continue.")
sys.exit()
try:
if 'temp' in wb.sheetnames:
wb.move_sheet("temp", -(len(wb.sheetnames)-1))
else:
wb.create_sheet('temp',0)
sheet = wb.worksheets[0]
file = open(csv_name,"r",encoding = "utf-8")
except Exception as e:
print("...............E R R O R ................." + str(e))
print()
# print(sheet)
isFile=os.path.isfile(csv_name)
if (isFile != csv_name):
print("CSV File " + csv_name + " Not Found")
sys.exit()
#rows and columns
# row data will append from last row
# if data exists, go to next row
if sheet.max_row > 1:
row=sheet.max_row + 1
else :
# if no data on sheet, start from row 1
row=sheet.max_row
print("Starting at row, " + str(row))
column = 1
#for each line in the csv file
for line in file:
#remove the \n from the line and make it a list with the separator
line = line[:-1]
line = line.split(sep)
#for each data in the line
for data in line:
sheet.cell(row,column).value = data
column += 1
# to write the next row column number is set to 1 and row number is increased by 1
column = 1
row += 1
print("Ending on row " + (str((row-1))))
#saving the excel file and closing the csv file
wb.save(excel_name)
file.close()
sys.stdout.flush
def here_is_more():
pass
if __name__ == '__main__':
main()