def fasterSolution(folderAddress, pic): document = Document() COLUMNS = 4 table = document.add_table(rows=1000, columns=COLUMNS) table_cells = table._cells for i in range(ROWS): row_cells = table_cells[i * COLUMNS:(i + 1) * COLUMNS] for cell in row_cells.cells: paragraph = cell.paragraphs[0] run = paragraph.add_run() run.add_picture(folderAddress + pic + ".png", width=350000 * 1.42, height=700000 * 0.49)
def putIntoDocumentFiles(folderAddress, pic): document = Document() table = document.add_table(rows=100, cols=4) #ROWS=25 For 100 barcodes for row in table.rows: for cell in row.cells: paragraph = cell.paragraphs[0] run = paragraph.add_run() run.add_picture( folderAddress + "\\" + pic + ".png", width=350000 * 0.71, height=350000 * 0.49 ) #(width,height)=>dimensions(singleTableRow,singleTableColumn) for singlePage document.save(folderAddress + "\\singleFolder" + "\\" + pic + "_" + "1" + ".docx")
def fasterSolution(folderAddress, pic, progress): progress['value'] = time.time() - start_time progress.update() document = Document() COLUMNS = 4 ROWS = 1000 table = document.add_table(rows=ROWS, cols=COLUMNS) table_cells = table._cells for i in range(ROWS): row_cells = table_cells[i * COLUMNS:(i + 1) * COLUMNS] for cell in row_cells: paragraph = cell.paragraphs[0] run = paragraph.add_run() run.add_picture(folderAddress + "\\" + pic + ".png", width=350000 * 0.71, height=350000 * 0.49) progress['value'] = time.time() - start_time progress.update() document.save(folderAddress + "\\" + "singleFolder" + "\\" + pic + "_" + str(1) + ".docx") progress['value'] = time.time() - start_time progress.update()
# headers; these will become the keys of our dictionary if i == 0: keys = tuple(text) continue # Construct a dictionary for this row, mapping # keys to values for this row row_data = tuple(text) data.append(row_data) for hero in heroes: hero_doc = Document() hero_doc.add_heading(hero, level=1) table = hero_doc.add_table(rows=1, cols=4) table.style = 'TableGrid' hdr_cells = table.rows[0].cells hdr_cells[0].text = unicode('Номер сцены', 'utf-8') hdr_cells[0].width = 1097280 hdr_cells[1].text = unicode('Описание', 'utf-8') hdr_cells[1].width = 4846320 hdr_cells[2].text = unicode('Персонажи', 'utf-8') hdr_cells[2].width = 2423160 hdr_cells[3].text = unicode('Примечания', 'utf-8') hdr_cells[3].width = 20583680 for row in data: if hero in u''.join(row[4]): row_cells = table.add_row().cells row_cells[0].width = 1097280
class ThreadingExample(object): """ Threading example class The run() method will be started and it will run in the background until the application exits. """ #function downloads youtube video with audio or silence according to a variable named youtube_audio_var #if empty url is inserted or wrong pattern matching url is inserted, throws error to textBox accordingly def download_youtube_video_with_audio(self): video_url = self.youtube_url_var.get() yav = self.youtube_audio_var.get() if video_url == '': self.textBox.insert(1.0, "Empty URL inserted" + "\n") else: p = re.compile( r'^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+') m = p.match(video_url) if m: try: if yav == True: stream1 = YouTube(video_url).streams.filter( progressive=yav) if stream1 == [] or stream1 == None: self.textBox.insert( 1.0, "Choose without audio instead of progressive(video and audio):" + "\n") else: stream1[0].download(self.folderAddress) self.textBox.insert( 1.0, "Video finished downloading: " + video_url + " : " + "with audio :" + str(yav) + "\n") elif yav == False: stream2 = YouTube(video_url).streams.filter( adaptive=not yav) if stream2 == [] or stream2 == None: self.textBox.insert( 1.0, "Choose with audio instead of adaptive(video):" + "\n") else: stream2[0].download(self.folderAddress) #YouTube(video_url).streams.first().download(self.folderAddress) self.textBox.insert( 1.0, "Video finished downloading: " + video_url + " : " + "without audio :" + str(not yav) + "\n") except Exception as e: self.textBox.insert(1.0, str(e) + "\n") self.youtube_url.delete(0, 'end') else: self.textBox.insert(1.0, "Invalid url for download inserted" + "\n") #function downloads youtube video between the available resolutions #if empty url is inserted or wrong pattern matching url is inserted, throws error to textBox accordingly def download_youtube_video(self): video_url = self.youtube_url_var.get() youtube_res = self.youtube_var.get() if video_url == '': self.textBox.insert(1.0, "Empty URL inserted" + "\n") else: p = re.compile( r'^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+') m = p.match(video_url) if m: try: stream = YouTube(video_url).streams.get_by_resolution( youtube_res) if stream == [] or stream == None: self.textBox.insert( 1.0, "Choose another resolution instead of :" + youtube_res + "\n") else: stream.download() #YouTube(video_url).streams.first().download(self.folderAddress) self.textBox.insert( 1.0, "Video finished downloading: " + video_url + " : " + youtube_res + "\n") except Exception as e: self.textBox.insert(1.0, str(e) + "\n") self.youtube_url.delete(0, 'end') else: self.textBox.insert(1.0, "Invalid url for download inserted" + "\n") #function makes a folder named singleFolder if doesn't exists and put corresponding documents of barcodes #code_128 barcodes are taken as input from two label above and then given as input to another function #sub functions are defined as putIntoDocumentFiles and a faster version of it def action(self): def makeFolder(self): try: os.mkdir(self.folderAddress + "\\" + self.directoryName) except FileExistsError: pass self.makeFolder = makeFolder(self) self.Code_128 = "code128" self.letter_words_1 = self.gender_var.get() self.letter_words_2 = self.gender_var_2.get() def fasterSolution(self, b): self.document = Document() self.COLUMNS = 4 self.ROWS = 100 self.table = self.document.add_table(rows=self.ROWS, cols=self.COLUMNS) self.table_cells = self.table._cells for i in range(self.ROWS): self.row_cells = self.table_cells[i * self.COLUMNS:(i + 1) * self.COLUMNS] for cell in self.row_cells: self.paragraph = cell.paragraphs[0] self.run = self.paragraph.add_run() self.run.add_picture(self.folderAddress + "\\" + self.b + ".png", width=350000 * 0.71, height=350000 * 0.49) self.document.save(self.folderAddress + "\\" + "singleFolder" + "\\" + self.b + "_" + str(1) + ".docx") #print(f'{input_1} and {input_2}') self.pageNumber = int(self.page_var.get()) self.array, self.array2 = [], [] #print('here') for i in range(1, 1 + self.pageNumber): self.barcode_Name = self.letter_words_1 + self.letter_words_2 + "00" + str( i) self.ean = barcode.get(self.Code_128, self.barcode_Name, writer=ImageWriter()) #------------------------ #print(self.folderAddress+"\\"+self.barcode_Name) self.textBox.insert( 1.0, self.folderAddress + "\\" + self.barcode_Name + "\n") self.filename = self.ean.save( self.folderAddress + "\\" + self.barcode_Name ) #saves to [MG001.png,MG002.png...MG00(pageNumber).png] self.array.append(self.folderAddress + "\\" + self.barcode_Name + ".png") self.array2.append(self.barcode_Name) makeFolder(self) for a, b in zip(self.array, self.array2): self.b = b #self.putIntoDocumentFiles=putIntoDocumentFiles(self,b) #self.putIntoDocumentFiles(self,b) self.fasterSolution = fasterSolution(self, self.b) fasterSolution(self, self.b) self.textBox.insert( 1.0, "Done:" + self.folderAddress + "\\" + self.directoryName + "\\" + self.b + "_" + str(1) + ".docx" + "\n") def create_thread_version_Two(self, function_name): self.run_thread = Thread(target=function_name) self.run_thread.setDaemon(True) # <=== add this line self.run_thread.start() self.textBox.insert(1.0, str(self.run_thread) + "\n") self.textBox.insert(1.0, str(self.run_thread.isAlive()) + "\n") # def create_thread(self): self.run_thread = Thread(target=self.action) self.run_thread.setDaemon(True) # <=== add this line self.run_thread.start() #------------------- #print(self.run_thread) #print('createThread():',self.run_thread.isAlive()) self.textBox.insert(1.0, str(self.run_thread) + "\n") self.textBox.insert(1.0, str(self.run_thread.isAlive()) + "\n") #------------------- def create_thread_two(self): self.run_thread = Thread(target=self.download_youtube_video) self.run_thread.setDaemon(True) # <=== add this line self.run_thread.start() self.textBox.insert(1.0, str(self.run_thread) + "\n") self.textBox.insert(1.0, str(self.run_thread.isAlive()) + "\n") def create_thread_three(self): self.run_thread = Thread(target=self.download_youtube_video_with_audio) self.run_thread.setDaemon(True) # <=== add this line self.run_thread.start() self.textBox.insert(1.0, str(self.run_thread) + "\n") self.textBox.insert(1.0, str(self.run_thread.isAlive()) + "\n") #function sets title to Untitled and deletes everything from textBox def new_file(self, event=None): self.win.title("Untitled") global file_name file_name = None self.textBox.delete(1.0, END) #on_content_changed() #function opens file from devices, sets title to opened file name and insert text into textBox def open_file(self, event=None): input_file_name = tkinter.filedialog.askopenfilename( defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")]) if input_file_name: global file_name file_name = input_file_name self.win.title('{} - {}'.format(os.path.basename(file_name), 'GUI')) self.textBox.delete(1.0, END) with open(file_name) as _file: self.textBox.insert(1.0, _file.read()) #on_content_changed() #function writes text from textBox into file_name def write_to_file(self, file_name): try: content = self.textBox.get(1.0, 'end') with open(file_name, 'w') as the_file: the_file.write(content) except IOError: tkinter.messagebox.showwarning("Save", "Could not save the file.") #function takes file name as input and then runs subfunctions and also sets title to saved file path def save_as(self, event=None): input_file_name = tkinter.filedialog.asksaveasfilename( defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")]) if input_file_name: #global self.file_name self.file_name = input_file_name self.write_to_file(self.file_name) self.win.title('{} - {}'.format(os.path.basename(self.file_name), 'GUI')) return "break" #function saves file_name if not exists as save as function, else write_to_file function def save(self, event=None): #global self.file_name if not self.file_name: self.save_as() else: self.write_to_file(self.file_name) return "break" def exit_editor(self, event=None): if tkinter.messagebox.askokcancel("Quit?", "Really quit?"): self.win.destroy() def undo(self): self.textBox.event_generate("<<Undo>>") #on_content_changed() return "break" def redo(self, event=None): self.textBox.event_generate("<<Redo>>") #on_content_changed() return 'break' #function finds text that matches the pattern inserted into the entry widget and then does a search by #giving it to another function named search_output def find_text(self, event=None): self.search_toplevel = Toplevel(self.win) self.search_toplevel.title('Find Text') self.search_toplevel.transient(self.win) Label(self.search_toplevel, text="Find All:").grid(row=0, column=0, sticky='e') self.search_entry_widget = Entry(self.search_toplevel, width=25) self.search_entry_widget.grid(row=0, column=1, padx=2, pady=2, sticky='we') self.search_entry_widget.focus_set() self.ignore_case_value = IntVar() Checkbutton(self.search_toplevel, text='Ignore Case', variable=self.ignore_case_value).grid(row=1, column=1, sticky='e', padx=2, pady=2) Button(self.search_toplevel, text="Find All", underline=0, command=lambda: self.search_output( self.search_entry_widget.get(), self.ignore_case_value.get( ), self.textBox, self.search_toplevel, self. search_entry_widget)).grid(row=0, column=2, sticky='e' + 'w', padx=2, pady=2) #function searches for a needle in a haystack accordingly to search custom function of textBox def search_output(self, needle, if_ignore_case, content_text, search_toplevel, search_box): self.textBox.tag_remove('match', '1.0', END) matches_found = 0 if needle: start_pos = '1.0' while True: start_pos = self.textBox.search(needle, start_pos, nocase=if_ignore_case, stopindex=END) if not start_pos: break end_pos = '{}+{}c'.format(start_pos, len(needle)) self.textBox.tag_add('match', start_pos, end_pos) matches_found += 1 start_pos = end_pos self.textBox.tag_config('match', foreground='red', background='yellow') search_box.focus_set() search_toplevel.title('{} matches found'.format(matches_found)) #function creates menubar with submenus as file menubar and edit menubar correspondingly def create_menubar(self): self.new_file_icon = PhotoImage(file='icons/new_file.gif') self.open_file_icon = PhotoImage(file='icons/open_file.gif') self.save_file_icon = PhotoImage(file='icons/save.gif') self.cut_icon = PhotoImage(file='icons/cut.gif') self.copy_icon = PhotoImage(file='icons/copy.gif') self.paste_icon = PhotoImage(file='icons/paste.gif') self.undo_icon = PhotoImage(file='icons/undo.gif') self.redo_icon = PhotoImage(file='icons/redo.gif') self.menu_bar = Menu(self.win) self.file_menu = Menu(self.menu_bar, tearoff=0) self.file_menu.add_command(label='New', accelerator='Ctrl+N', compound='left', image=self.new_file_icon, underline=0, command=self.new_file) self.file_menu.add_command(label='Open', accelerator='Ctrl+O', compound='left', image=self.open_file_icon, underline=0, command=self.open_file) self.file_menu.add_command(label='Save', accelerator='Ctrl+S', compound='left', image=self.save_file_icon, underline=0, command=self.save) self.file_menu.add_command(label='Save as', accelerator='Shift+Ctrl+S', command=self.save_as) self.file_menu.add_separator() self.file_menu.add_command(label='Exit', accelerator='Alt+F4', command=self.exit_editor) self.menu_bar.add_cascade(label='File', menu=self.file_menu) self.edit_menu = Menu(self.menu_bar, tearoff=0) self.edit_menu.add_command(label='Undo', accelerator='Ctrl+Z', compound='left', image=self.undo_icon, command=self.undo) self.edit_menu.add_command(label='Redo', accelerator='Ctrl+Y', compound='left', image=self.redo_icon, command=self.redo) self.edit_menu.add_separator() self.edit_menu.add_command(label='Find', underline=0, accelerator='Ctrl+F', command=self.find_text) self.menu_bar.add_cascade(label='Edit', menu=self.edit_menu) self.win.config(menu=self.menu_bar) #function create widgets like Label, Combobox, Entry and Button def create_widget(self): #made a label at zero row and zero column to select input between A-Z at the corresponding combobox self.gender_label = ttk.Label(self.win, text='Select input 1 : ') self.gender_label.grid(row=0, column=0, sticky=tk.W) self.gender_var = tk.StringVar() self.gender_combobox = ttk.Combobox(self.win, width=14, textvariable=self.gender_var, state='readonly') self.gender_combobox['values'] = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z') self.gender_combobox.current(0) self.gender_combobox.grid(row=0, column=1) #made a label at first row and zero column to select input between A-Z at the corresponding combobox self.gender_label_2 = ttk.Label(self.win, text='Select input 2 : ') self.gender_label_2.grid(row=1, column=0, sticky=tk.W) self.gender_var_2 = tk.StringVar() self.gender_combobox_2 = ttk.Combobox(self.win, width=14, textvariable=self.gender_var_2, state='readonly') self.gender_combobox_2['values'] = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z') self.gender_combobox_2.current(0) self.gender_combobox_2.grid(row=1, column=1) #made a label at second row and zero column to take corresponding entry as input between 1-inf self.page_label = ttk.Label(self.win, text='Until page number starting from 1: ') self.page_label.grid(row=2, column=0, sticky=tk.W) self.page_var = tk.IntVar() self.page_number = Entry(bd=5, textvariable=self.page_var) self.page_number.grid(row=2, column=1, sticky=tk.W) #submit button is attached to function action here which is inside create_thread function self.submit_button = ttk.Button(self.win, text='print Barcodes', command=self.create_thread) self.submit_button.grid(row=4, column=0) #made a label at fifth row and zero column that takes youtube url as input self.youtube_label = ttk.Label(self.win, text='Give youtube url : ') self.youtube_label.grid(row=5, column=0, sticky=tk.W) self.youtube_url_var = tk.StringVar() self.youtube_url = Entry(bd=5, textvariable=self.youtube_url_var) self.youtube_url.grid(row=5, column=1, sticky=tk.W) self.youtube_var = tk.StringVar() self.youtube_combobox = ttk.Combobox(self.win, width=14, textvariable=self.youtube_var, state='readonly') self.youtube_combobox['values'] = ("720p", "480p", "360p", "240p", "144p") self.youtube_combobox.current(0) self.youtube_combobox.grid(row=5, column=2) #made a label at fifth row and zero column that takes youtube url with or without audio as input self.youtube_label_2 = ttk.Label( self.win, text='Prefer it with audio or silence : ') self.youtube_label_2.grid(row=5, column=3, sticky=tk.W) self.youtube_audio_var = tk.BooleanVar() self.youtube_combobox_2 = ttk.Combobox( self.win, width=14, textvariable=self.youtube_audio_var, state='readonly') self.youtube_combobox_2['values'] = (True, False) self.youtube_combobox_2.current(0) self.youtube_combobox_2.grid(row=5, column=4) #youtube button is attached to function download_youtube_video here which is inside create_thread_2 function self.youtube_button = ttk.Button( self.win, text='download youtube video from a url', command=self.create_thread_two) self.youtube_button.grid(row=6, column=0) #WRONG WAY OF DOING IT #self.youtube_button_1=ttk.Button(self.win,text='download youtube video from a url with audio or not',command=self.create_thread_version_Two(self.download_youtube_video_with_audio)) self.youtube_button_1 = ttk.Button( self.win, text='download youtube video from a url with audio or not', command=self.create_thread_three) self.youtube_button_1.grid(row=6, column=2) self.textBox = ScrolledText(self.win, borderwidth=3, relief="sunken") self.textBox.grid(column=0, row=7, columnspan=6, rowspan=1, sticky='W') def __init__(self, interval=1): """ Constructor :type interval: int :param interval: Check interval, in seconds """ self.stop = 0 self.interval = interval self.win = tk.Tk() self.win.geometry('800x500') self.win.title('GUI') self.directoryName = "singleFolder" self.folderAddress = os.getcwd() #self.start_thread=start_thread #self.stopped=stopped self.create_widget() self.file_name = None self.create_menubar() self.win.mainloop()
def make_docx(infile): #generate facture number and filename for saving facture_n, outfile = create_facture_n() #step 1: get info from text commande_n, reference_n, total_ht, total_tva, total_ttc, tasks, prices = get_contents(infile) #step 2b: append the reference number to the end of the pdf file move_pdf(infile, reference_n) #step 3: make the doc file #prepare shadings for 2 final table cells shading_1 = parse_xml(r'<w:shd {} w:fill="#99ccff"/>'.format(nsdecls('w'))) shading_2 = parse_xml(r'<w:shd {} w:fill="#99ccff"/>'.format(nsdecls('w'))) newdoc = Document() #Title info p1 = newdoc.add_paragraph() p1.add_run('Tom Viart').bold = True p1.add_run(''' 132 rue du Chemin Vert 75011 PARIS Tél : 06 47 59 86 17''') p1.add_run(''' Identifiant SIRET : 75105892600021 N° TVA : FR 68 751058926''').bold = True p2 = newdoc.add_paragraph('À l’attention d’') p2.add_run('ALTO').bold = True p2.add_run(''' 99, rue du Faubourg Saint Martin 75010 PARIS ''') p2.alignment = WD_ALIGN_PARAGRAPH.RIGHT today = datetime.datetime.today().strftime('%d/%m/%Y') p3 = newdoc.add_paragraph() p3.add_run('FACTURE %d' % facture_n).bold = True p3.add_run(''' En date du %s''' % today) p3.alignment = WD_ALIGN_PARAGRAPH.CENTER #make the table of prices table = newdoc.add_table(rows=5, cols=2) table.style = 'Table Grid' table.alignment = WD_ALIGN_PARAGRAPH.CENTER # format cell widths for cell in table.columns[0].cells: cell.width = Inches(4.0) for cell in table.columns[1].cells: cell.width = Inches(0.9) a = table.cell(0, 0) b = table.cell(0,1) A = a.merge(b) tt = 'Commande N° %s \nRéférence %s\n' % (commande_n,reference_n) A2 = A.add_paragraph(tt) A2.alignment = WD_ALIGN_PARAGRAPH.CENTER c = table.cell(1,0) c.text = tasks d = table.cell(1,1) d.text = prices d.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER e = table.cell(2,0) e.text = "Total HT" e.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER e2 = table.cell(2,1) e2.text = total_ht e2.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER f = table.cell(3,0) f.text = "TVA 20 %" f.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER f2 = table.cell(3,1) f2.text = total_tva f2.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER g = table.cell(4,0) g.text = "Total TTC" g.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER g._tc.get_or_add_tcPr().append(shading_1) g2 = table.cell(4,1) g2.text = total_ttc g2.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER g2._tc.get_or_add_tcPr().append(shading_2) ### End of document -- does not change newdoc.add_paragraph('''\n\nValeur en votre aimable règlement à réception (RIB ci-joint) ''') newdoc.add_paragraph('''\nRELEVE D’IDENTITE BANCAIRE''') table2 = newdoc.add_table(rows=2, cols=4) table2.style = 'Table Grid' a = table2.cell(0,0) a.text = "Banque" a = table2.cell(0,1) a.text = "Guichet" a = table2.cell(0,2) a.text = "Numéro de compte" a = table2.cell(0,3) a.text = "Clé RIB" b = table2.cell(1,0) b.text = "10278" b = table2.cell(1,1) b.text = "06041" b = table2.cell(1,2) b.text = "00020864601" b = table2.cell(1,3) b.text = "54" newdoc.add_paragraph(''' IDENTIFICATION INTERNATIONALE''') table3 = newdoc.add_table(rows=1, cols=1) table3.style = 'Table Grid' a = table3.cell(0,0) a.text = '''IBAN : FR76 1027 8060 4100 0208 6460 154 BIC : CMCIFR2A''' newdoc.save(outfile) out_location = os.path.abspath(os.path.join(os.getcwd(), outfile)) # Update the facture xls document using update facture() function try: update_facture(facture_n, reference_n, total_ht) print(" Created file: ",outfile) except: print("!! Error making excel file for {}".format(infile)) #print("\n".join([commande_n, reference_n, total_ht, total_tva, total_ttc, tasks, prices])) #open DOC file open_file_for_tom(r'C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE', out_location) #create a warning message if not all values were found if '' in [commande_n, reference_n, total_ht, total_tva, total_ttc, tasks, prices]: from easygui import msgbox msgbox(msg='Error: program did not correctly find all values', title='Error', ok_button='OK', image=None, root=None) errors = pd.DataFrame([commande_n, reference_n, total_ht, total_tva, total_ttc, tasks, prices],columns = ['commande_n', 'reference_n', 'total_ht', 'total_tva', 'total_ttc', 'tasks', 'prices']) errors.to_csv('errors.csv',index=False)
plt.savefig(imgname) # generate word document = Document() document.add_heading('Data analysis Report', level=0) first_student = students.iloc[0, :]['Name'] first_score = students.iloc[0, :]['Score'] p = document.add_paragraph('The highest score is student ') p.add_run(str(first_student)).bold = True p.add_run(', score is ') p.add_run(str(first_score)).bold = True p1 = document.add_paragraph( f'Totally {len(students.Name)} students attended the test, the summary of the testing is:' ) table = document.add_table(rows=len(students.Name) + 1, cols=2) table.style = 'LightShading-Accent1' table.cell(0, 0).text = 'Student Name' table.cell(0, 1).text = 'Student Score' for i, (index, row) in enumerate(students.iterrows()): table.cell(i + 1, 0).text = str(row['Name']) table.cell(i + 1, 1).text = str(row['Score']) document.add_picture(imgname) document.save('Students.docx') print('Done!!')
class ThreadingExample(object): """ Threading example class The run() method will be started and it will run in the background until the application exits. """ ## function makes a folder named singleFolder if doesn't exists and put corresponding documents of barcodes ## code_128 barcodes are taken as input from two label above and then given as input to another function ## sub functions are defined as putIntoDocumentFiles and a faster version of it def action(self): while True: print("hello") if self.stop == 1: break self.progress = Progressbar(self.win, orient="horizontal", length=300, mode='determinate') self.progress.grid(row=6) self.progress["maximum"] = 100 self.start_time = time.time() #Starting progress bar here self.progress.start() self.directoryName = "singleFolder" self.folderAddress = os.getcwd() self.progress['value'] = time.time() - self.start_time self.progress.update() def makeFolder(self): try: os.mkdir(self.folderAddress + "\\" + self.directoryName) except FileExistsError: pass self.makeFolder = makeFolder(self) self.Code_128 = "code128" self.letter_words_1 = self.gender_var.get() self.letter_words_2 = self.gender_var_2.get() self.progress['value'] = time.time() - self.start_time self.progress.update() def fasterSolution(self, b): self.progress['value'] = time.time() - self.start_time self.progress.update() self.document = Document() self.COLUMNS = 4 self.ROWS = 1000 self.table = self.document.add_table(rows=self.ROWS, cols=self.COLUMNS) self.table_cells = self.table._cells for i in range(self.ROWS): self.row_cells = self.table_cells[i * self.COLUMNS:(i + 1) * self.COLUMNS] for cell in self.row_cells: self.paragraph = cell.paragraphs[0] self.run = self.paragraph.add_run() self.run.add_picture(self.folderAddress + "\\" + self.b + ".png", width=350000 * 0.71, height=350000 * 0.49) self.progress['value'] = time.time() - self.start_time self.progress.update() self.document.save(self.folderAddress + "\\" + "singleFolder" + "\\" + self.b + "_" + str(1) + ".docx") self.progress['value'] = time.time() - self.start_time self.progress.update() """def putIntoDocumentFiles(self,b): self.document = Document() self.table=document.add_table(rows=100,cols=4)#ROWS=25 For 100 barcodes for row in self.table.rows: for cell in row.cells: self.paragraph=cell.paragraphs[0] self.run=self.paragraph.add_run() self.run.add_picture(self.folderAddress+"\\"+self.b+".png",width=350000*0.71,height=350000*0.49)#(width,height)=>dimensions(singleTableRow,singleTableColumn) for singlePage self.document.save(self.folderAddress+"\\singleFolder"+"\\"+self.b+"_"+"1"+".docx") #for i in range(2): #document.save(folderAddress+"\\singleFolder"+"\\"+pic+"_"+str(i+1)+".docx") """ #print(f'{input_1} and {input_2}') self.pageNumber = int(self.page_var.get()) self.array, self.array2 = [], [] #print('here') for i in range(1, 1 + self.pageNumber): self.progress['value'] = time.time() - self.start_time self.progress.update() self.barcode_Name = self.letter_words_1 + self.letter_words_2 + "00" + str( i) self.ean = barcode.get(self.Code_128, self.barcode_Name, writer=ImageWriter()) print(self.folderAddress + "\\" + self.barcode_Name) self.filename = self.ean.save( self.folderAddress + "\\" + self.barcode_Name ) #saves to [MG001.png,MG002.png...MG00(pageNumber).png] self.array.append(self.folderAddress + "\\" + self.barcode_Name + ".png") self.array2.append(self.barcode_Name) makeFolder(self) for a, b in zip(self.array, self.array2): self.b = b self.progress['value'] = time.time() - self.start_time self.progress.update() #self.putIntoDocumentFiles=putIntoDocumentFiles(self,b) #self.putIntoDocumentFiles(self,b) self.fasterSolution = fasterSolution(self, self.b) fasterSolution(self, self.b) self.progress['value'] = time.time() - self.start_time self.progress.update() #Stopping progress bar here self.progress.stop() def start_thread(self): global stop self.stop = 0 t = Thread(target=self.action) t.start() def stopped(self): global stop self.stop = 1 def __init__(self, interval=1): """ Constructor :type interval: int :param interval: Check interval, in seconds """ self.stop = 0 self.interval = interval self.win = tk.Tk() self.win.geometry('350x350') self.win.title('GUI') #self.start_thread=start_thread #self.stopped=stopped #made a label at zero row and zero column to select input between A-Z at the corresponding combobox self.gender_label = ttk.Label(self.win, text='Select input 1 : ') self.gender_label.grid(row=0, column=0, sticky=tk.W) self.gender_var = tk.StringVar() self.gender_combobox = ttk.Combobox(self.win, width=14, textvariable=self.gender_var, state='readonly') self.gender_combobox['values'] = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z') self.gender_combobox.current(0) self.gender_combobox.grid(row=0, column=1) #made a label at first row and zero column to select input between A-Z at the corresponding combobox self.gender_label_2 = ttk.Label(self.win, text='Select input 2 : ') self.gender_label_2.grid(row=1, column=0, sticky=tk.W) self.gender_var_2 = tk.StringVar() self.gender_combobox_2 = ttk.Combobox(self.win, width=14, textvariable=self.gender_var_2, state='readonly') self.gender_combobox_2['values'] = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z') self.gender_combobox_2.current(0) self.gender_combobox_2.grid(row=1, column=1) #made a label at second row and zero column to take corresponding entry as input between 1-inf self.page_label = ttk.Label(self.win, text='Until page number starting from 1: ') self.page_label.grid(row=2, column=0, sticky=tk.W) self.page_var = tk.IntVar() self.page_number = Entry(bd=5, textvariable=self.page_var) self.page_number.grid(row=2, column=1, sticky=tk.W) #self.thread = threading.Thread(name="action",target=self.action) #self.thread.daemon = False # Daemonize thread #thread.start() # Start the execution #submit button is attached to function action here self.submit_button = ttk.Button(self.win, text='print Barcodes', command=self.start_thread) self.submit_button.grid(row=4, column=0) self.stop_button = ttk.Button(self.win, text='Stop', command=self.stopped) self.stop_button.grid(row=5, column=0) self.win.mainloop()