Print many pages only by one button with a template

I have design a software for the warehouse to print deliver paper for customer.
There is a column is ‘Selected’ ,if this column is 1, my logic will send my power table’s Sales order to a templates then print, like the following code

#Detect how many columns my table have
while number< row:
	if PyData[number]['Config']==1:
		#check if the selected status is 1
		#if yes , i will send the SalesOrder to the print label formate
		if PyData[number]['PrintPcs']==2:
		elif PyData[number]['PrintPcs']==1:
		#According to the logic print 1 PCS OR 2 PCS
	#Go to check the next line

The problem is while I click this button. This template will don’t change during the logic work

Can you give some inputs to improve the logic to realize like the following picture when go to the next line, the data can be send to the template during the logic is working

Your script is running on the ‘Event Dispatch Thread’, meaning nothing in the GUI will update while your script is running (loops, especially while loops, are very dangerous in scripting for this reason).

You need to invoke an asynchronous script to perform your actual printing, I suspect; however, you may run into issues with races, so you may need to restructure your logic a bit. If you look through the forum, there’s some good resources on invoking asynchronous tasks.