1- import os
2- import time
3-
4- from openpyxl import load_workbook , Workbook
5- from openpyxl .worksheet .properties import WorksheetProperties , PageSetupProperties
6- from openpyxl .styles import Font
7-
8- def main ():
9- """Main function of script"""
10-
11- intro ()
12-
13- time_file = input ("Enter file (don't forget .xlsx): " )
14- date = input ("Enter date - Day/Month/Year\n (write \" now\" to use todays date): " )
15- hours = input ("Enter worked hours: " )
16- project = input ("Enter project name: " )
17-
18- logToExcel (time_file , [date , hours , project ])
19-
20- def intro ():
21- """Intro to the script"""
22- print ("\n This is a worked hours tracker, made by Felix Khoi.\n " )
23- print ("This script will save given date, hours and project to given excel-file." )
24- print ("If the file doesn't exist, it will create a new excel-file.\n " )
25-
26- def logToExcel (time_file , worked_hours ):
27- """Function that logs to excel-file"""
28-
29- print ("\n Starting to search for file..." )
30-
31- #Checks if file already exists
32- if (os .path .isfile (time_file )):
33- print ("\n Found " + time_file + "!" )
34-
35- #Loading excel file
36- time_tracker_sheet = load_workbook (filename = time_file )
37-
38- #Activates writing for given file
39- writer = time_tracker_sheet .active
40- else :
41- #If file doesn't exist, create new excel-file
42- time_tracker_sheet = Workbook ()
43-
44- #Activates writer for new excel-file
45- writer = time_tracker_sheet .active
46-
47- #Writes headers into new excel-file
48- writer ['A1' ] = "Date"
49- writer ['B1' ] = "Number of hours"
50- writer ['C1' ] = "Project"
51-
52- #Adding bold and increasing font size
53- writer ['A1' ].font = Font (bold = True , size = 16 )
54- writer ['B1' ].font = Font (bold = True , size = 16 )
55- writer ['C1' ].font = Font (bold = True , size = 16 )
56-
57- date = worked_hours [0 ]
58-
59- #Checks if user wants to use today's date
60- if (worked_hours [0 ] == "now" ):
61- #Gets today's date
62- date = time .strftime ("%d/%m/%Y" )
63-
64- #Appends worked hours to excel-file
65- writer .append ([date , worked_hours [1 ], worked_hours [2 ]])
66-
67- #Sets the width of the columns "A", "B", "C"
68- writer .column_dimensions ["A" ].width = 50.0
69- writer .column_dimensions ["B" ].width = 50.0
70- writer .column_dimensions ["C" ].width = 50.0
71-
72- #Saves excel-file
73- time_tracker_sheet .save (time_file )
74-
75- print ("\n Done, saved to " + time_file + "\n " )
76-
77-
78- if __name__ == "__main__" :
79- main ()
1+ """
2+ Python to Excel
3+ ================
4+
5+ This example scripts is written by Felix Khoi and will go over how you can write from Python to Excel.
6+
7+ The example will contain two versions, one simple and one more advanced.
8+ In the simple one you will have to manually write code to write to Excel, it just contains a example of how you do it.
9+ The more advanced script will show a user dialog where it lets the user select excel-file, date, hours and project.
10+
11+ OpenPyXL
12+ ===========
13+
14+ The script uses the import [OpenPyXL](https://openpyxl.readthedocs.org/en/default/).
15+ """
16+
17+
18+ import os
19+ import time
20+
21+ from openpyxl import load_workbook , Workbook
22+ from openpyxl .styles import Font
23+
24+ def main ():
25+ """Main function of script"""
26+
27+ intro ()
28+
29+ time_file = input ("Enter file (don't forget .xlsx): " )
30+ date = input ("Enter date - Day/Month/Year\n (write \" now\" to use todays date): " )
31+ hours = input ("Enter worked hours: " )
32+ project = input ("Enter project name: " )
33+
34+ logToExcel (time_file , [date , hours , project ])
35+
36+ def intro ():
37+ """Intro to the script"""
38+ print ("\n This is a worked hours tracker, made by Felix Khoi.\n " )
39+ print ("This script will save given date, hours and project to given excel-file." )
40+ print ("If the file doesn't exist, it will create a new excel-file.\n " )
41+
42+ def logToExcel (time_file , worked_hours ):
43+ """Function that logs to excel-file"""
44+
45+ print ("\n Starting to search for file..." )
46+
47+ #Checks if file already exists
48+ if os .path .isfile (time_file ):
49+ print ("\n Found " + time_file + "!" )
50+
51+ #Loading excel file
52+ time_tracker_sheet = load_workbook (filename = time_file )
53+
54+ #Activates writing for given file
55+ writer = time_tracker_sheet .active
56+ else :
57+ #If file doesn't exist, create new excel-file
58+ time_tracker_sheet = Workbook ()
59+
60+ #Activates writer for new excel-file
61+ writer = time_tracker_sheet .active
62+
63+ #Writes headers into new excel-file
64+ writer ['A1' ] = "Date"
65+ writer ['B1' ] = "Number of hours"
66+ writer ['C1' ] = "Project"
67+
68+ #Adding bold and increasing font size
69+ writer ['A1' ].font = Font (bold = True , size = 16 )
70+ writer ['B1' ].font = Font (bold = True , size = 16 )
71+ writer ['C1' ].font = Font (bold = True , size = 16 )
72+
73+ date = worked_hours [0 ]
74+
75+ #Checks if user wants to use today's date
76+ if worked_hours [0 ] == "now" :
77+ #Gets today's date
78+ date = time .strftime ("%d/%m/%Y" )
79+
80+ #Appends worked hours to excel-file
81+ writer .append ([date , worked_hours [1 ], worked_hours [2 ]])
82+
83+ #Sets the width of the columns "A", "B", "C"
84+ writer .column_dimensions ["A" ].width = 50.0
85+ writer .column_dimensions ["B" ].width = 50.0
86+ writer .column_dimensions ["C" ].width = 50.0
87+
88+ #Saves excel-file
89+ time_tracker_sheet .save (time_file )
90+
91+ print ("\n Done, saved to " + time_file + "\n " )
92+
93+ if __name__ == "__main__" :
94+ main ()
0 commit comments