Skip to content

Instantly share code, notes, and snippets.

@exemplum100
Created July 16, 2023 07:31
Show Gist options
  • Save exemplum100/4f9d85ac4d4299f34793f464f7959f9d to your computer and use it in GitHub Desktop.
Save exemplum100/4f9d85ac4d4299f34793f464f7959f9d to your computer and use it in GitHub Desktop.
Пример использования python c excel
import openpyxl as px
from tkinter import *
from tkinter import filedialog as fd
import os
import re
file_name='C:/Users/Default/Desktop/PyScript/PyScript/отчет.xlsx'
defrow=1
defcolumn=1
sheetnms=''
sheetl=[]
def scan():
global sheetl
sheetl=[]
#e1.delete(0,END)
#e2.delete(0,END)
#добавить проверку на заголовки
#добавить проверку на столбцы
#добавить проверку на листы
global defrow
global defcolumn
global sheetnms
pin100=px.load_workbook(file_name)
sheetlist=pin100.get_sheet_names()
es=0
for j in sheetlist:
y=[]
defrow=1
decolumn=1
sheet=pin100.get_sheet_by_name(j)
colsrch=sheet.max_column
for i in range(1,colsrch+1):
x=sheet.cell(row=defrow,column=i).value
x=str(x)
rgxpin=re.compile(r'^\d+$')
y=rgxpin.findall(x)
if y:
sheetl2=[]
defcolumn=i
defrow=1
es+=1
sheetl2.append(j)
sheetl2.append(defrow)
sheetl2.append(defcolumn)
sheetl.append(sheetl2)
break
if y==[]:
defrow=2
for i in range(1,colsrch+1):
x=sheet.cell(row=defrow,column=i).value
x=str(x)
rgxpin=re.compile(r'^\d+$')
y=rgxpin.findall(x)
if y:
sheetl2=[]
defcolumn=i
es+=1
sheetl2.append(j)
sheetl2.append(defrow)
sheetl2.append(defcolumn)
sheetl.append(sheetl2)
break
if es==0:
lstr3['text']='ПИН-кодов не найдено'
#e1.insert(0,str(defrow))
#e2.insert(0,str(defcolumn))
lstr3['text']='Найденные листы с пинами \n (лист, строка, столбец): \n' + str(sheetl)
print(sheetl)
def openfl():
global file_name
global sheetnms
file_name = fd.askopenfilename()
pin100=px.load_workbook(file_name)
basesheet=[pin100.get_sheet_names()[0],1,1]
sheetl.append(basesheet)
lstr3['text']='Выбран файл: \n' +os.path.basename(file_name)
print(file_name)
print(sheetl)
def rezult():
il=0
ik=0
print(sheetl)
if file_name !='':
a=Toplevel()
a.title("Результат")
a.geometry('+210+210')
pin100=px.load_workbook(file_name)
if len(sheetl)>1:
y=''
pins=[]
for i2 in sheetl:
print('Условие + ' + str(i2))
sheet=pin100.get_sheet_by_name(i2[0])
for i in range(i2[1],sheet.max_row+1):
pins.append(sheet.cell(row=i, column=i2[2]).value)
for i in range(len(pins)):
x='\''+str(pins[i])+'\''
if i != len(pins)-1:
y+=x+','
else:
y+=x
text= Text(a,width=100, height=100)
text.insert(1.0,y)
text.pack()
if len(sheetl)<=1:
sheet=pin100.get_sheet_by_name(sheetl[0][0])
pins=[]
y=''
for i in range(sheetl[0][1],sheet.max_row+1):
pins.append(sheet.cell(row=i, column=sheetl[0][2]).value)
for i in range(len(pins)):
x='\''+str(pins[i])+'\''
if i != len(pins)-1:
y+=x+','
else:
y+=x
text= Text(a,width=100, height=100)
text.insert(1.0,y)
text.pack()
else:
lstr3['text']='Файл не выбран'
root = Tk()
root.geometry('1000x800')
root.title("Превращение ПИНов")
b1=Button(text='Открыть', command=openfl).place(relx=0.3,rely=0.4)
b2=Button(text='Результат', command=rezult).place(relx=0.6,rely=0.4)
b3=Button(text='Сканирование', command=scan).place(relx=0.6,rely=0.1)
#e1=Entry(width=3)
#e1.place(relx=0.35,rely=0.1)
#e2=Entry(width=3)
#e2.place(relx=0.35,rely=0.2)
#l1=Label(text='Строка').place(relx=0.2, rely=0.1)
#l2=Label(text='Колонка').place(relx=0.2, rely=0.2)
lstr3=Label(text='')
lstr3.place(relx=0.15, rely=0.65)
root.mainloop()
#Начало выборки
pin100=px.load_workbook(file_name)
sheet=pin100.get_sheet_by_name(pin100.get_sheet_names()[0])
c=sheet['A1'].value
print(sheet.title)
print(pin100.get_sheet_names())
print(pin100.get_sheet_names()[0])
print(sheet)
print(sheet.max_row)
pins=[]
for i in range(1,sheet.max_row+1):
pins.append(sheet.cell(row=i, column=1).value)
print(pins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment