Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import time
- import docx2pdf
- import docx.shared
- from docx import *
- import datetime
- import openpyxl
- import barcode
- from barcode.writer import ImageWriter
- import os
- from pyzbar import pyzbar
- import cv2
- class compInfo:
- def __new__(cls, *args, **kwargs):
- return super().__new__(cls)
- def __init__(self, receiver):
- self.sender = 'СУПЕРФИРМА'
- self.receiver = receiver
- self.reason = ''
- self.n_number = ''
- self.n_date = ''
- self.items = []
- def __iter__(self):
- return self
- def compile(self):
- time = datetime.date.today()
- self.n_date = str(time.day) + "." + str(time.month) + '.' + str(time.year)
- def clear():
- for file in os.listdir(os.getcwd()):
- if re.search(r'(накладная)|(AB)', file):
- os.remove(file)
- def parse():
- complist = []
- wb = openpyxl.load_workbook("data_isukk_8.xlsx")['27.03.2022']
- tmp = []
- for r in range(2, 71):
- tmp.append(wb.cell(r, 2).value)
- tmp = list(set(tmp))
- for comp in tmp:
- complist.append(compInfo(comp))
- for r in range(2, 71):
- for i, c in enumerate(complist):
- if wb.cell(r, 2).value == c.receiver:
- c.reason = wb.cell(r, 3).value
- c.n_number = i
- c.compile()
- tmp = [wb.cell(r, 4).value, wb.cell(r, 5).value, wb.cell(r, 6).value, wb.cell(r, 7).value,
- wb.cell(r, 6).value * wb.cell(r, 7).value]
- c.items.append(tmp)
- break
- return complist
- def brcode(title):
- code128 = barcode.get('code128', title, writer=ImageWriter())
- code128.default_writer_options['module_height'] = 3.0
- code128.default_writer_options['module_width'] = 0.05
- pic = code128.save(title)
- return title
- def generate_nacl():
- template = 'template.docx'
- data = parse()
- for c in data:
- template_doc = Document(template)
- style = template_doc.styles['Normal']
- font = style.font
- font.name = 'Times New Roman'
- font.size = docx.shared.Pt(14)
- file = 'AB_' + str(c.n_number) + '_' + c.n_date
- brcode(file)
- r = template_doc.paragraphs[0].add_run()
- r.add_picture(file + '.png', width=docx.shared.Cm(10))
- os.remove(file + '.png')
- for paragraph in template_doc.paragraphs:
- replace_text(paragraph, '{{N_NUMBER}}', str(c.n_number))
- replace_text(paragraph, '{{N_DATE}}', c.n_date)
- replace_text(paragraph, '{{SENDER}}', c.sender)
- replace_text(paragraph, '{{RECEIVER}}', c.receiver)
- replace_text(paragraph, '{{REASON}}', c.reason)
- table = template_doc.tables[0]
- row_i = 1
- sum_quant = 0
- total = 0
- for item, e, p, q, s in c.items:
- table.add_row()
- table.rows[row_i].cells[0].text = str(row_i)
- table.rows[row_i].cells[1].text = str(item)
- table.rows[row_i].cells[2].text = str(e)
- table.rows[row_i].cells[3].text = str(p)
- table.rows[row_i].cells[4].text = str(q)
- table.rows[row_i].cells[5].text = str(s)
- row_i += 1
- sum_quant += int(q)
- total += int(s)
- for paragraph in template_doc.paragraphs:
- replace_text(paragraph, '{{SUM_QUANT}}', str(sum_quant))
- replace_text(paragraph, '{{TOTAL}}', str(total))
- template_doc.save('накладная_' + str(c.n_number) + '_' + str(c.receiver) + '.docx')
- def replace_text(paragraph, key, value):
- if key in paragraph.text:
- paragraph.text = paragraph.text.replace(key, value)
- def generate_lists():
- data = parse()
- template = 'template_list.docx'
- for c in data:
- template_doc = Document(template)
- style = template_doc.styles['Normal']
- font = style.font
- font.name = 'Times New Roman'
- font.size = docx.shared.Pt(14)
- file = 'AB_' + str(c.n_number) + '_' + c.n_date
- brcode(file)
- r = template_doc.paragraphs[0].add_run()
- r.add_picture(file + '.png', width=docx.shared.Cm(10))
- os.remove(file + '.png')
- for paragraph in template_doc.paragraphs:
- replace_text(paragraph, '{{N_NUMBER}}', str(c.n_number))
- replace_text(paragraph, '{{N_DATE}}', c.n_date)
- replace_text(paragraph, '{{SENDER}}', c.sender)
- replace_text(paragraph, '{{RECEIVER}}', c.receiver)
- replace_text(paragraph, '{{REASON}}', c.reason)
- table = template_doc.tables[0]
- row_i = 1
- sum_quant = 0
- total = 0
- for item, e, p, q, s in c.items:
- table.add_row()
- table.rows[row_i].cells[0].text = str(row_i)
- table.rows[row_i].cells[1].text = str(item)
- table.rows[row_i].cells[2].text = str(e)
- table.rows[row_i].cells[3].text = str(q)
- row_i += 1
- sum_quant += int(q)
- total += int(s)
- template_doc.save('AB_' + str(c.n_number) + '_' + str(c.receiver) + '.docx')
- docx2pdf.convert('AB_' + str(c.n_number) + '_' + str(c.receiver) + '.docx',
- 'AB_' + str(c.n_number) + '_' + str(c.receiver) + '.pdf')
- os.remove('AB_' + str(c.n_number) + '_' + str(c.receiver) + '.docx')
- return 0
- def draw_barcode(decoded, image):
- image = cv2.rectangle(image, (decoded.rect.left, decoded.rect.top),
- (decoded.rect.left + decoded.rect.width, decoded.rect.top +
- decoded.rect.height),
- color=(0, 255, 0), thickness=5)
- return image
- def decode(image):
- decoded_objects = pyzbar.decode(image)
- for obj in decoded_objects:
- image = draw_barcode(obj, image)
- # print("Type:", obj.type)
- # print("Data:", obj.data.decode("utf-8"))
- # print()
- for file in os.listdir(os.getcwd()):
- if re.search(normalize(str(obj.data.decode("utf-8"))), file):
- os.startfile(file)
- break
- time.sleep(15)
- return 0
- def normalize(s: str):
- a = s.split("_")
- return a[1]
- if __name__ == '__main__':
- # clear()
- # generate_nacl()
- # generate_lists()
- cap = cv2.VideoCapture(0)
- while True:
- _, frame = cap.read()
- decoded_objects = decode(frame)
- cv2.imshow("frame", frame)
- captured = pyzbar.decode(frame)
- if cv2.waitKey(1) == ord('q'):
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement