From 3e5104aa7af3997612764b6ed3157070328f662f Mon Sep 17 00:00:00 2001 From: ppinko Date: Mon, 10 Feb 2020 20:27:40 +0100 Subject: [PATCH 1/2] Initial commit --- Task_6/word_template.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Task_6/word_template.py diff --git a/Task_6/word_template.py b/Task_6/word_template.py new file mode 100644 index 0000000..fbfa372 --- /dev/null +++ b/Task_6/word_template.py @@ -0,0 +1,33 @@ +import docx +import os + +date = str(input("Please enter today's date: ")) +name = str(input("Please enter your full name: ")).upper() +roll_number = str(input("Please enter your roll number: ")) +year = str(input("Please enter year: ")) +leave_days = str(input("Please enter number of requested leave days: ")) +start_day = str(input("Please enter start day of the leave: ")) +end_day = str(input("Please enter end day of the leave: ")) +name = 'PAWEL PINKOWICZ' + +doc = docx.Document('test.docx') +num_par = doc.paragraphs +for i in range(len(num_par)): + print(i, doc.paragraphs[i].text) + +date_par = doc.paragraphs[2].add_run(' {0}'.format(date)) +name_par = doc.paragraphs[4].add_run(' {0}'.format(name)) +roll_number_par = doc.paragraphs[5].add_run(' {0}'.format(roll_number)) +year_par = doc.paragraphs[6].add_run(' {0}'.format(year)) +doc.save('new.docx') + +main_text = doc.paragraphs[16].text +main_text = main_text.replace('____', leave_days, 1) +main_text = main_text.replace('____________', start_day, 1) +main_text = main_text.replace('___________', end_day, 1) +doc.paragraphs[16].text = main_text +#words = main_text.rsplit() +print(main_text) +# print(words) + +doc.save('new.docx') \ No newline at end of file From e9dc4dc796bd114046e2de358b02e394f96e85d2 Mon Sep 17 00:00:00 2001 From: ppinko Date: Mon, 10 Feb 2020 23:01:57 +0100 Subject: [PATCH 2/2] Task no. 6 solution, connected to the job #97 --- Task_6/word_template.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Task_6/word_template.py b/Task_6/word_template.py index fbfa372..a11504c 100644 --- a/Task_6/word_template.py +++ b/Task_6/word_template.py @@ -1,6 +1,8 @@ import docx import os +import shutil +print("") date = str(input("Please enter today's date: ")) name = str(input("Please enter your full name: ")).upper() roll_number = str(input("Please enter your roll number: ")) @@ -8,26 +10,34 @@ leave_days = str(input("Please enter number of requested leave days: ")) start_day = str(input("Please enter start day of the leave: ")) end_day = str(input("Please enter end day of the leave: ")) -name = 'PAWEL PINKOWICZ' +print("") -doc = docx.Document('test.docx') -num_par = doc.paragraphs -for i in range(len(num_par)): - print(i, doc.paragraphs[i].text) +# Starting connection with the template +orignal_file = 'Leave Letter Format.docx' +original_path = os.path.join(os.getcwd(), orignal_file) +doc = docx.Document(original_path) +# Inserting date, name, roll number and year date_par = doc.paragraphs[2].add_run(' {0}'.format(date)) name_par = doc.paragraphs[4].add_run(' {0}'.format(name)) roll_number_par = doc.paragraphs[5].add_run(' {0}'.format(roll_number)) year_par = doc.paragraphs[6].add_run(' {0}'.format(year)) -doc.save('new.docx') +# Editing main part of the template including leave days, start day and end day main_text = doc.paragraphs[16].text main_text = main_text.replace('____', leave_days, 1) main_text = main_text.replace('____________', start_day, 1) main_text = main_text.replace('___________', end_day, 1) doc.paragraphs[16].text = main_text -#words = main_text.rsplit() -print(main_text) -# print(words) -doc.save('new.docx') \ No newline at end of file +# Saving a new file +new_file_name = 'Leave Letter {0}.docx'.format(name) +path_new_file = os.path.join(os.getcwd(), new_file_name) +shutil.copy2(original_path, path_new_file) # complete target filename given +doc.save(new_file_name) + +# Checking output file (new file) +doc2 = docx.Document(new_file_name) +num_par = doc.paragraphs +for i in range(len(num_par)): + print(doc2.paragraphs[i].text) \ No newline at end of file