88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
|
import pathlib
|
|||
|
from sqlite3 import Row
|
|||
|
|
|||
|
import requests
|
|||
|
from openpyxl import load_workbook, Workbook
|
|||
|
from openpyxl.cell import Cell
|
|||
|
from openpyxl.worksheet.worksheet import Worksheet
|
|||
|
from pathlib import Path
|
|||
|
import pandas as pd
|
|||
|
|
|||
|
API_CALC_URL = "https://api.jde.ru/vD/calculator/PriceAddress"
|
|||
|
TYPE = "1"
|
|||
|
PICKUP = "1"
|
|||
|
DELIVERY = "1"
|
|||
|
USER = "2252130929823409"
|
|||
|
TOKEN = "67065749269910593"
|
|||
|
|
|||
|
|
|||
|
class ExcelParser:
|
|||
|
def __init__(self, path: Path | str):
|
|||
|
self._wb: Workbook | None = None
|
|||
|
if isinstance(path, str):
|
|||
|
path = Path(path)
|
|||
|
|
|||
|
assert path.is_file() is True
|
|||
|
self.convert_to_xlsx(path)
|
|||
|
self._sheet: Worksheet = self._wb.active
|
|||
|
|
|||
|
def convert_to_xlsx(self, path: pathlib.Path):
|
|||
|
import pyexcel as pe
|
|||
|
if path.suffix == '.xlsx':
|
|||
|
self._wb = load_workbook(str(path))
|
|||
|
return
|
|||
|
pe.save_book_as(file_name=str(path), dest_file_name=str(path.with_suffix('.xlsx')))
|
|||
|
self._wb = load_workbook(str(path.with_suffix('.xlsx')))
|
|||
|
|
|||
|
def clean_up_wb(self) -> pd.DataFrame:
|
|||
|
triggered = False
|
|||
|
for row in self._sheet.rows:
|
|||
|
for cell in row:
|
|||
|
cell: Cell
|
|||
|
row: Row
|
|||
|
if isinstance(cell.value, str) and cell.value.startswith('№ заявки'):
|
|||
|
self._sheet.delete_rows(1, cell.row - 1)
|
|||
|
triggered = True
|
|||
|
break
|
|||
|
|
|||
|
if triggered:
|
|||
|
break
|
|||
|
|
|||
|
df = pd.DataFrame(self._sheet.values)
|
|||
|
not_nullable_cols = df.iloc[0].dropna().index
|
|||
|
df = df.iloc[:, not_nullable_cols]
|
|||
|
df.columns = df.iloc[0, :]
|
|||
|
df: pd.DataFrame = df.drop(0, axis=0)
|
|||
|
|
|||
|
df.to_excel('./text.xlsx')
|
|||
|
|
|||
|
return df[['Адрес отгрузки', 'Адрес склада', 'Масса', 'Объем']].to_dict(orient='records')[0]
|
|||
|
|
|||
|
def calculate(self) -> int | None:
|
|||
|
df = self.clean_up_wb()
|
|||
|
query = {
|
|||
|
"type": TYPE,
|
|||
|
"token": TOKEN,
|
|||
|
"delivery": DELIVERY,
|
|||
|
"pickup": PICKUP,
|
|||
|
"user": USER,
|
|||
|
"addr_from": df['Адрес отгрузки'],
|
|||
|
"addr_to": df['Адрес склада'],
|
|||
|
"weight": df['Масса'],
|
|||
|
"volume": df['Объем'],
|
|||
|
}
|
|||
|
if query['volume'] is None:
|
|||
|
query['volume'] = 0.01
|
|||
|
print(query)
|
|||
|
data = requests.get(API_CALC_URL, params=query).json()
|
|||
|
print(data)
|
|||
|
|
|||
|
if data.get('price', None) is not None:
|
|||
|
return int(data['price'])
|
|||
|
return None
|
|||
|
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
parser = ExcelParser('./downloads/1342AWP1A.xls')
|
|||
|
print(parser.calculate())
|