Spaces:
Sleeping
Sleeping
Update excel_process.py
Browse files- excel_process.py +119 -0
excel_process.py
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from openpyxl import Workbook
|
| 4 |
+
from openpyxl import load_workbook
|
| 5 |
+
from openpyxl.styles import Alignment
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
def get_ctnr_num(input_path):
|
| 10 |
+
raw_data = input_path.split('-')
|
| 11 |
+
file_name = raw_data[-1].split('.')
|
| 12 |
+
ctnr_number = file_name[0]
|
| 13 |
+
return ctnr_number.strip()
|
| 14 |
+
|
| 15 |
+
def get_short_invoice(invoice):
|
| 16 |
+
raw_data = invoice.split('-')
|
| 17 |
+
short_invoice = f"{raw_data[1]}-{raw_data[2]}"
|
| 18 |
+
return short_invoice
|
| 19 |
+
|
| 20 |
+
def create_new_excel(path):
|
| 21 |
+
if os.path.exists(path):
|
| 22 |
+
return
|
| 23 |
+
wb = Workbook()
|
| 24 |
+
sheet = wb.active
|
| 25 |
+
sheet.title = "Draft DAS"
|
| 26 |
+
wb.save(path)
|
| 27 |
+
|
| 28 |
+
def set_sheet_col_style(sheet):
|
| 29 |
+
sheet.column_dimensions['A'].width=20
|
| 30 |
+
sheet.column_dimensions['B'].width=10
|
| 31 |
+
sheet.column_dimensions['C'].width=20
|
| 32 |
+
sheet.column_dimensions['D'].width=10
|
| 33 |
+
sheet.column_dimensions['E'].width=30
|
| 34 |
+
sheet.column_dimensions['F'].width=20
|
| 35 |
+
sheet.column_dimensions['G'].width=20
|
| 36 |
+
sheet.column_dimensions['H'].width=20
|
| 37 |
+
max_rows = sheet.max_row # 获取最大行
|
| 38 |
+
max_columns = sheet.max_column # 获取最大列
|
| 39 |
+
for i in range(1, max_rows + 1):
|
| 40 |
+
for j in range(1, max_columns + 1):
|
| 41 |
+
sheet.cell(i, j).alignment = Alignment(horizontal='center',vertical='center')
|
| 42 |
+
|
| 43 |
+
def excel_process(input_path, output_path, invoice):
|
| 44 |
+
wb = load_workbook(input_path)
|
| 45 |
+
sheets = wb.worksheets # 获取当前所有的sheet
|
| 46 |
+
print(sheets)
|
| 47 |
+
|
| 48 |
+
# 获取第一张sheet
|
| 49 |
+
sheet = sheets[0]
|
| 50 |
+
print(sheet)
|
| 51 |
+
short_invoice = get_short_invoice(invoice)
|
| 52 |
+
ctnr_number = get_ctnr_num(input_path)
|
| 53 |
+
company = sheet['A1'].value
|
| 54 |
+
report_name = sheet['A2'].value
|
| 55 |
+
module_type = sheet['B4'].value
|
| 56 |
+
customer = sheet['E4'].value
|
| 57 |
+
date_val = sheet['J4'].value
|
| 58 |
+
|
| 59 |
+
data = {
|
| 60 |
+
"Voc": [sheet['B5'].value, sheet['B6'].value, sheet['B7'].value],
|
| 61 |
+
"Isc": [sheet['C5'].value, sheet['C6'].value, sheet['C7'].value],
|
| 62 |
+
"Vpm": [sheet['D5'].value, sheet['D6'].value, sheet['D7'].value],
|
| 63 |
+
"Ipm": [sheet['E5'].value, sheet['E6'].value, sheet['E7'].value],
|
| 64 |
+
"Pm": [sheet['F5'].value, sheet['F6'].value, sheet['F7'].value],
|
| 65 |
+
"FF":[sheet['G5'].value, sheet['G6'].value, sheet['G7'].value],
|
| 66 |
+
}
|
| 67 |
+
df_stat = pd.DataFrame(data)
|
| 68 |
+
|
| 69 |
+
max_row = sheet.max_row
|
| 70 |
+
max_col = sheet.max_column
|
| 71 |
+
print(f"max_row:{max_row}, max_col:{max_col}")
|
| 72 |
+
|
| 73 |
+
df = pd.DataFrame(columns=['Module_ID', 'Voc', 'Isc', 'Vpm','Ipm', 'Pm', 'FF', 'Watt_Marking', 'Pallet_ID', 'Current_level', 'Shipment_date', 'License_plate_number'])
|
| 74 |
+
|
| 75 |
+
for i in range(9, max_row+1):
|
| 76 |
+
row_data = []
|
| 77 |
+
for j in range(1, max_col+1):
|
| 78 |
+
val = sheet.cell(i,j).value
|
| 79 |
+
if val is None:
|
| 80 |
+
break
|
| 81 |
+
row_data.append(sheet.cell(i,j).value)
|
| 82 |
+
if row_data == []:
|
| 83 |
+
break
|
| 84 |
+
df.loc[i-9]=row_data
|
| 85 |
+
print(df)
|
| 86 |
+
Pallet_ID_counts = df['Pallet_ID'].value_counts()
|
| 87 |
+
print(Pallet_ID_counts)
|
| 88 |
+
|
| 89 |
+
if not os.path.exists(output_path):
|
| 90 |
+
create_new_excel(output_path)
|
| 91 |
+
wb_out = load_workbook(output_path)
|
| 92 |
+
sheet = wb_out.active
|
| 93 |
+
head = ['PRODUCT','PCS','PALLET ID','LOCATION','INVOICE/CTNR_NUMBER','WAREHOUSE REF IN','CONSIGNEE','ORDER NO']
|
| 94 |
+
tail = ['','','','','','','','']
|
| 95 |
+
sheet.append(head)
|
| 96 |
+
for id, count in Pallet_ID_counts.items():
|
| 97 |
+
PRODUCT = module_type
|
| 98 |
+
PCS = count
|
| 99 |
+
PALLET_ID = id
|
| 100 |
+
LOCATION = 'RWB'
|
| 101 |
+
INVOICE_CTNR_NUMBER = f'{short_invoice}/{ctnr_number}'
|
| 102 |
+
WAREHOUSE_REF_IN = 'FREE'
|
| 103 |
+
CONSIGNEE = 'DASSOLFRA'
|
| 104 |
+
ORDER_NO = invoice
|
| 105 |
+
data = [PRODUCT, PCS, PALLET_ID, LOCATION, INVOICE_CTNR_NUMBER, WAREHOUSE_REF_IN, CONSIGNEE, ORDER_NO]
|
| 106 |
+
sheet.append(data)
|
| 107 |
+
sheet.append(tail)
|
| 108 |
+
set_sheet_col_style(sheet)
|
| 109 |
+
wb_out.save(output_path)
|
| 110 |
+
|
| 111 |
+
if __name__=="__main__":
|
| 112 |
+
input_path = 'tmpw6er8scq/Flash Report-720pcs-600W-2024-2-27 - CSNU7329467.xlsx'
|
| 113 |
+
output_path = 'tmpw6er8scq/result.xlsx'
|
| 114 |
+
invoice = 'DASBH-N15-240104'
|
| 115 |
+
excel_process(input_path, output_path, invoice)
|
| 116 |
+
# wb = load_workbook('/home/yang/sda/github/das/output/Receive Upload_BL No.COSU1710812845.xlsx')
|
| 117 |
+
# sheet = wb.active # 获取当前所有的sheet
|
| 118 |
+
# set_sheet_col_style(sheet)
|
| 119 |
+
# wb.save('/home/yang/sda/github/das/output/Receive Upload_BL No.COSU1710812845.xlsx')
|