Advertisement
Gamerkin

isuk 10-2

Apr 24th, 2024
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. import sqlite3
  2. from sqlite3 import Error
  3. import openpyxl
  4. from datetime import datetime
  5. import pprint
  6.  
  7. current_day = datetime.now().strftime('%d.%m.%Y')
  8.  
  9. def sql_connection():
  10.   try:
  11.     con = sqlite3.connect('mydatabase.db')
  12.     return con
  13.   except Error:
  14.     print(Error)
  15.  
  16. def sql_table(con):
  17.   cursorObj = con.cursor()
  18.   ex = 'CREATE TABLE documents(id integer PRIMARY KEY, doc_num text, \
  19.        company_from text, company_to text, \
  20.        reason text, item text, type text, \
  21.        amount int, status text, date text)'
  22.   cursorObj.execute(ex)
  23.   con.commit()
  24.  
  25. def sql_insert(con, entities):
  26.   cursorObj = con.cursor()
  27.   ex = 'INSERT INTO documents(doc_num, company_from, company_to, reason, item, \
  28.  type, amount, status, date) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)'
  29.   cursorObj.execute(ex, entities)
  30.   con.commit()
  31.  
  32.  
  33. def write_data_from_xlsx(con, filename):
  34.   wb = openpyxl.load_workbook(filename)
  35.   ws = wb.active
  36.   companys = {}
  37.   num = 1
  38.   for j in range(2, ws.max_row+1):
  39.     company = ws.cell(row=j, column=2).value
  40.     if company not in companys.keys():
  41.       companys.update({company: f'AB_{num}_{current_day}'})
  42.       num += 1
  43.     data = (companys[company], ws.cell(row=j, column=1).value, ws.cell(row=j, column=2).value,
  44.              ws.cell(row=j, column=3).value,
  45.              ws.cell(row=j, column=4).value, ws.cell(row=j, column=5).value,
  46.              ws.cell(row=j, column=7).value,
  47.              "выдан", current_day)
  48.     sql_insert(con, data)  
  49.  
  50.  
  51. con = sql_connection()
  52. sql_table(con)
  53. write_data_from_xlsx(con, "data.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement