# -*- coding: utf-8 -*-
# powers 20210512 GTK
import pandas as pd
import os
#os.environ['MATPLOTLIBDATA'] = 'D:/Program Files/Python36/Lib/site-packages/matplotlib/mpl-data'
#import numpy as np
#坐标文件
road_ax = input('please input positon_info xlsx:')
layer_data = pd.DataFrame(pd.read_excel(format(road_ax), sheet_name=0))
#原始bom
road_bom = input('please input bom_data xlsx:')
bom_data = pd.DataFrame(pd.read_excel(format(road_bom), sheet_name=0))
# 根据坐标文件获取top and bot信息
layer_dict = {}
i = 0
while i < layer_data.shape[0]:
layer_dict[layer_data.iloc[i,0]] = layer_data.iloc[i,1]
i = i + 1
#新建 bot and top 格式BOM
data_bom_goertek_bot = pd.DataFrame(columns=(
'Item', 'GTK_PART_NUMBER', 'Name', 'Describe',
'MF_PART_NUMBER', 'MANUFACTURER', 'Quantity', 'Reference', 'Layer'))
data_bom_goertek_top = pd.DataFrame(columns=(
'Item', 'GTK_PART_NUMBER', 'Name', 'Describe',
'MF_PART_NUMBER', 'MANUFACTURER', 'Quantity', 'Reference', 'Layer'))
#print(bom_data.shape[0])
j = 0
bot_reference = []
top_reference = []
while j < bom_data.shape[0]:
# 获取原始bom 数据
reference = bom_data.iloc[j,7]
reference = reference.split(',')
Item = bom_data.iloc[j,0]
GTK_PART_NUMBER = bom_data.iloc[j,1]
Name = bom_data.iloc[j,2]
Describe = bom_data.iloc[j,3]
MF_PART_NUMBER = bom_data.iloc[j,4]
MANUFACTURER = bom_data.iloc[j,5]
try:
# 区分 bot and top
for id in reference:
if layer_dict[id] == 'YES':
bot_reference.append(id)
elif layer_dict[id] == 'NO':
top_reference.append(id)
else:
pass
# 根据 bot and top 制作BOM_data
if len(bot_reference) > 0:
Quantity = len(bot_reference)
bot = ','.join(bot_reference)
data_bom_goertek_bot.loc[data_bom_goertek_bot.shape[0]] = (Item,GTK_PART_NUMBER,Name,Describe,MF_PART_NUMBER,MANUFACTURER,str(Quantity),bot,'BOT')
if len(top_reference) > 0:
Quantity = len(top_reference)
top = ','.join(top_reference)
data_bom_goertek_top.loc[data_bom_goertek_top.shape[0]] = (Item,GTK_PART_NUMBER,Name,Describe,MF_PART_NUMBER,MANUFACTURER,str(Quantity),top,'TOP')
j = j + 1
bot_reference = []
top_reference = []
except Exception as e:
print(e)
print('error,please check the positon_info and BOM_DATA')
break
# 合并bot and top bom_data
data_bom_goertek = pd.concat([data_bom_goertek_top,data_bom_goertek_bot])
# 保存bom
try:
with pd.ExcelWriter('bom_top_bot.xlsx') as writer:
data_bom_goertek.to_excel(writer)
writer.save()
print('BOM done,please check the bom,if the script report some error,maybe the bom is not right,thanks')
except Exception as e:
print(e)