import pandas as pd
import tkinter as tk
from tkinter import filedialog
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
import os
import shutil
def select_input_file():
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx;*.xls")], title="请选择输入数据excel,.xlsx格式")
return file_path
def select_template_file():
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx;*.xls")], title="请选择工艺卡模版的excel,.xlsx格式")
return file_path
def select_target_folder():
folder_path = filedialog.askdirectory(title="请选择输出文件夹")
return folder_path
def process01_df(df_in):
for index, row in df_in.iterrows():
if row['焊接类型'] == '对焊' or row['焊接类型'] == '承插焊':
diam_thickness = f"Φ{row['主管外径']}*{row['壁厚']}"
df_in.at[index, '外径壁厚'] = diam_thickness
elif row['焊接类型'] == '开口焊':
diam_thickness = f"Φ{row['支管外径']}*{row['支管壁厚']}-Φ{row['主管外径']}*{row['壁厚']}"
df_in.at[index, '外径壁厚'] = diam_thickness
# 提取前段材质和前段材质编号
if "A105" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "A105"
df_in.at[index, '前材质编码'] = 20
elif "20-G" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "20"
df_in.at[index, '前材质编码'] = 21
elif "20G-" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "20G"
df_in.at[index, '前材质编码'] = 22
elif "20Ⅱ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "20Ⅱ"
df_in.at[index, '前材质编码'] = 23
elif "20Ⅲ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "20Ⅲ"
df_in.at[index, '前材质编码'] = 24
elif "12Cr1MoVG" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "12Cr1MoVG"
df_in.at[index, '前材质编码'] = 2
elif "12Cr1MoVⅡ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "12Cr1MoVⅡ"
df_in.at[index, '前材质编码'] = 3
elif "12Cr1MoVⅢ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "12Cr1MoVⅢ"
df_in.at[index, '前材质编码'] = 4
elif "15CrMoG" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "15CrMoG"
df_in.at[index, '前材质编码'] = 5
elif "15CrMoⅡ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "15CrMoⅡ"
df_in.at[index, '前材质编码'] = 6
elif "15CrMoⅢ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "15CrMoⅢ"
df_in.at[index, '前材质编码'] = 7
elif "S30408-" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "S30408"
df_in.at[index, '前材质编码'] = 11
elif "S30408Ⅱ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "S30408Ⅱ"
df_in.at[index, '前材质编码'] = 12
elif "S30408Ⅲ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "S30408Ⅲ"
df_in.at[index, '前材质编码'] = 13
elif "S31603-" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "S31603"
df_in.at[index, '前材质编码'] = 14
elif "S31603Ⅱ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "S31603Ⅱ"
df_in.at[index, '前材质编码'] = 15
elif "S31603Ⅲ" in row["焊缝前材质"]:
df_in.at[index, '前材质'] = "S31603Ⅲ"
df_in.at[index, '前材质编码'] = 16
else:
df_in.at[index, '前材质编码'] = 0
# 提取后段材质和前段材质编号
if "A105" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "A105"
df_in.at[index, '后材质编码'] = 20
elif "20-G" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "20"
df_in.at[index, '后材质编码'] = 21
elif "20G-" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "20G"
df_in.at[index, '后材质编码'] = 22
elif "20Ⅱ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "20Ⅱ"
df_in.at[index, '后材质编码'] = 23
elif "20Ⅲ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "20Ⅲ"
df_in.at[index, '后材质编码'] = 24
elif "12Cr1MoVG-" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "12Cr1MoVG"
df_in.at[index, '后材质编码'] = 2
elif "12Cr1MoVⅡ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "12Cr1MoVⅡ"
df_in.at[index, '后材质编码'] = 3
elif "12Cr1MoVⅢ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "12Cr1MoVⅢ"
df_in.at[index, '后材质编码'] = 4
elif "15CrMoG-" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "15CrMoG"
df_in.at[index, '后材质编码'] = 5
elif "15CrMoⅡ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "15CrMoⅡ"
df_in.at[index, '后材质编码'] = 6
elif "15CrMoⅢ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "15CrMoⅢ"
df_in.at[index, '后材质编码'] = 7
elif "S30408-" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "S30408"
df_in.at[index, '后材质编码'] = 11
elif "S30408Ⅱ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "S30408Ⅱ"
df_in.at[index, '后材质编码'] = 12
elif "S30408Ⅲ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "S30408Ⅲ"
df_in.at[index, '后材质编码'] = 13
elif "S31603-" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "S31603"
df_in.at[index, '后材质编码'] = 14
elif "S31603Ⅱ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "S31603Ⅱ"
df_in.at[index, '后材质编码'] = 15
elif "S31603Ⅲ" in row["焊缝后材质"]:
df_in.at[index, '后材质'] = "S31603Ⅲ"
df_in.at[index, '后材质编码'] = 16
else:
df_in.at[index, '后材质编码'] = 0
# 汇合成为材质列
df_in['材质'] = ''
df_in.loc[(df_in['前材质编码'] == 0) & (df_in['后材质编码'] == 0), '材质'] = ''
df_in.loc[(df_in['前材质编码'] == 0) & (df_in['后材质编码'] != 0), '材质'] = df_in['后材质']
df_in.loc[(df_in['前材质编码'] != 0) & (df_in['后材质编码'] == 0), '材质'] = df_in['前材质']
df_in.loc[(df_in['前材质编码'] != 0) & (df_in['后材质编码'] != 0) & (df_in['前材质编码'] == df_in['后材质编码']),\
'材质'] = df_in['前材质'].astype(str)
df_in.loc[(df_in['前材质编码'] != 0) & (df_in['后材质编码'] != 0) & (df_in['前材质编码'] < df_in['后材质编码']),\
'材质'] = df_in['前材质'].astype(str) + "&" + df_in['后材质'].astype(str)
df_in.loc[(df_in['前材质编码'] != 0) & (df_in['后材质编码'] != 0) & (df_in['前材质编码'] > df_in['后材质编码']),\
'材质'] = df_in['后材质'].astype(str) + "&" + df_in['前材质'].astype(str)
if row['管线寸口'] == '1/2"':
df_in.at[index, '寸口'] = 1
elif row['管线寸口'] == '3/4"':
df_in.at[index, '寸口'] = 1
elif row['管线寸口'] == '1"':
df_in.at[index, '寸口'] = 1
elif row['管线寸口'] == '11/4"':
df_in.at[index, '寸口'] = 1.25
elif row['管线寸口'] == '11/2"':
df_in.at[index, '寸口'] = 1.5
elif row['管线寸口'] == '2"':
df_in.at[index, '寸口'] = 2
elif row['管线寸口'] == '21/2"':
df_in.at[index, '寸口'] = 2.5
elif row['管线寸口'] == '3"':
df_in.at[index, '寸口'] = 3
elif row['管线寸口'] == '4"':
df_in.at[index, '寸口'] = 4
elif row['管线寸口'] == '5"':
df_in.at[index, '寸口'] = 5
elif row['管线寸口'] == '6"':
df_in.at[index, '寸口'] = 6
elif row['管线寸口'] == '8"':
df_in.at[index, '寸口'] = 8
elif row['管线寸口'] == '10"':
df_in.at[index, '寸口'] = 10
elif row['管线寸口'] == '12"':
df_in.at[index, '寸口'] = 12
elif row['管线寸口'] == '14"':
df_in.at[index, '寸口'] = 14
elif row['管线寸口'] == '16"':
df_in.at[index, '寸口'] = 16
elif row['管线寸口'] == '20"':
df_in.at[index, '寸口'] = 20
elif row['管线寸口'] == '24"':
df_in.at[index, '寸口'] = 24
df_in['道数'] = 1
return df_in
# 定义填充“工艺卡编号”的函数
def fill_process_card(row):
if row['焊接类型'] == '对焊' and '12Cr' in row['材质']:
if row['壁厚'] > 13:
return 'HKB12-06'
elif row['壁厚'] > 10:
return 'HKB12-05'
elif row['壁厚'] > 8:
return 'HKB12-04'
elif row['壁厚'] > 5.5:
return 'HKB12-03'
elif row['壁厚'] > 3:
return 'HKB12-02'
elif row['壁厚'] > 0:
return 'HKB12-01'
elif row['焊接类型'] == '承插焊' and '12Cr' in row['材质']:
if row['壁厚'] > 5.5:
return 'HKC12-03'
elif row['壁厚'] > 3:
return 'HKC12-02'
elif row['壁厚'] > 0:
return 'HKC12-01'
elif row['焊接类型'] == '开口焊' and '12Cr' in row['材质']:
if row['支管壁厚'] > 5:
return 'HKD12-03'
elif row['支管壁厚'] > 3:
return 'HKD12-02'
elif row['支管壁厚'] > 0:
return 'HKD12-01'
elif row['焊接类型'] == '对焊' and '15Cr' in row['材质']:
if row['壁厚'] > 13:
return 'HKB15-06'
elif row['壁厚'] > 10:
return 'HKB15-05'
elif row['壁厚'] > 8:
return 'HKB15-04'
elif row['壁厚'] > 5.5:
return 'HKB15-03'
elif row['壁厚'] > 3:
return 'HKB15-02'
elif row['壁厚'] > 0:
return 'HKB15-01'
elif row['焊接类型'] == '承插焊' and '15Cr' in row['材质']:
if row['壁厚'] > 5.5:
return 'HKC15-03'
elif row['壁厚'] > 3:
return 'HKC15-02'
elif row['壁厚'] > 0:
return 'HKC15-01'
elif row['焊接类型'] == '开口焊' and '15Cr' in row['材质']:
if row['支管壁厚'] > 5:
return 'HKD15-03'
elif row['支管壁厚'] > 3:
return 'HKD15-02'
elif row['支管壁厚'] > 0:
return 'HKD15-01'
elif row['焊接类型'] == '对焊' and ('304' in row['材质'] or '316' in row['材质']):
if row['壁厚'] > 7:
return 'HKB304-04'
elif row['壁厚'] > 5:
return 'HKB304-03'
elif row['壁厚'] > 3:
return 'HKB304-02'
elif row['壁厚'] > 0:
return 'HKB304-01'
elif row['焊接类型'] == '承插焊' and ('304' in row['材质'] or '316' in row['材质']):
if row['壁厚'] > 5.5:
return 'HKC304-03'
elif row['壁厚'] > 3:
return 'HKC304-02'
elif row['壁厚'] > 0:
return 'HKC304-01'
elif row['焊接类型'] == '开口焊' and ('304' in row['材质'] or '316' in row['材质']):
if row['支管壁厚'] > 6.5:
return 'HKD304-04'
elif row['支管壁厚'] > 5:
return 'HKD304-03'
elif row['支管壁厚'] > 3:
return 'HKD304-02'
elif row['支管壁厚'] > 0:
return 'HKD304-01'
elif row['焊接类型'] == '对焊' and '20' in row['材质']:
if row['壁厚'] > 13:
return 'HKB20-06'
elif row['壁厚'] > 10:
return 'HKB20-05'
elif row['壁厚'] > 8:
return 'HKB20-04'
elif row['壁厚'] > 5.5:
return 'HKB20-03'
elif row['壁厚'] > 3:
return 'HKB20-02'
elif row['壁厚'] > 0:
return 'HKB20-01'
elif row['焊接类型'] == '承插焊' and '20' in row['材质']:
if row['壁厚'] > 5.5:
return 'HKC20-03'
elif row['壁厚'] > 3:
return 'HKC20-02'
elif row['壁厚'] > 0:
return 'HKC20-01'
elif row['焊接类型'] == '开口焊' and '20' in row['材质']:
if row['壁厚'] > 8 and row['支管壁厚'] > 8:
return 'HKD20-09'
elif row['壁厚'] > 8 and row['支管壁厚'] > 6.5:
return 'HKD20-08'
elif row['壁厚'] > 8 and row['支管壁厚'] > 5:
return 'HKD20-07'
elif row['壁厚'] > 8 and row['支管壁厚'] > 3:
return 'HKD20-06'
elif row['壁厚'] > 8 and row['支管壁厚'] > 0:
return 'HKD20-05'
elif row['壁厚'] > 0 and row['支管壁厚'] > 6.5:
return 'HKD20-04'
elif row['壁厚'] > 0 and row['支管壁厚'] > 5:
return 'HKD20-03'
elif row['壁厚'] > 0 and row['支管壁厚'] > 3:
return 'HKD20-02'
elif row['壁厚'] > 0 and row['支管壁厚'] > 0:
return 'HKD20-01'
return ""
# 做一个将焊缝编号进行归总序列化的函数,用来函数内调用
def format_numbers(numbers):
if not numbers:
return ""
# 对数字列表进行排序
sorted_numbers = sorted(set(numbers))
# 初始化结果字符串和当前范围的起始值
result = []
start = sorted_numbers[0]
end = start
# 遍历排序后的数字列表,合并连续的数字范围
for num in sorted_numbers[1:]:
if num == end + 1:
end = num
else:
if start == end:
result.append(str(start))
else:
result.append(f"{start}~{end}")
start = end = num
# 处理最后一个范围
if start == end:
result.append(str(start))
else:
result.append(f"{start}~{end}")
# 将结果列表转换为字符串,用逗号分隔
return ",".join(result)
def copy_template_to_new_file(pipeline_number_copy, template_path_copy, output_folder_copy):
# 构建新的文件名
new_file = f"{pipeline_number_copy}.xlsx"
# 构建新文件的完整路径
#new_file_path = os.path.join(output_folder_copy, new_file)
new_file_path = f"{output_folder_copy}/{new_file}" #实测这样写格式没有问题的,可以读到实际位置。
# 复制模板文件到新文件路径
shutil.copy(template_path_copy, new_file_path)
# 加载源文件和新文件
workbook_source = load_workbook(template_path_copy)
workbook_new = load_workbook(new_file_path)
# 复制每个 sheet 的图片
for sheet_name in workbook_source.sheetnames:
source_sheet = workbook_source[sheet_name]
new_sheet = workbook_new[sheet_name]
# 提取图片并添加到新 sheet 中
for img in source_sheet._images:
new_img = Image(img.ref)
new_img.anchor = img.anchor # 保持原图的位置
new_sheet.add_image(new_img) # 添加到新表中
# 筛选出管线号对应的行
filtered_rows = df_merge[df_merge["管线号"] == pipeline_number_copy]
processcards = filtered_rows['工艺卡编号'].unique()
# 获取所有sheet名
sheet_names = workbook_new.sheetnames
# 遍历所有sheet名,检查并删除不需要的sheet
for sheet_name in sheet_names:
if 'H' in sheet_name and sheet_name not in processcards:
std = workbook_new[sheet_name] # 获取具体的sheet
workbook_new.remove(std) # 删除sheet
# 保存修改后的Excel文件
workbook_new.save(new_file_path)
"""
这里加代码
"""
#尝试取得外径壁厚的值
# 筛选出管线号对应的行
filtered_rows_OD1 = df_merge[df_merge["管线号"] == pipeline_number_copy]
processcards = filtered_rows['工艺卡编号'].unique()
workbook_new = load_workbook(new_file_path)
#sheet3.cell((7 + i), 1).value = unique_result_list[i][0]
workbook_new["封面"].cell(17,9) .value = pipeline_number_copy.removeprefix(r"/")
count_sheet = 1
for sheet3 in workbook_new:
#sheet_name.cell(8,1,"好的")
#sheet_name.cell(9,1,"不好")
if 'H' in sheet3.title:
cover = workbook_new["封面"].cell(5, 13).value
sheet3.cell(2, 14).value = f"{cover}-{count_sheet}"
#sheet3['C8'] = "11"
#sheet3['C9'] = "12"
filtered_rows_OD = df_merge[(df_merge['管线号'] == pipeline_number_copy) & (df_merge['工艺卡编号'] == sheet3.title)]
# 提取“外径壁厚”和“焊接材质”列的值并存储到列表中
result_list = []
bianhao = []
for index, row in filtered_rows_OD.iterrows():
result_list.append((row['材质'], row['外径壁厚']))
bianhao.append(row['焊缝编号'])
unique_result_list = list(set(result_list))
txt_bianhao = format_numbers(bianhao)
txt_bianhao = txt_bianhao.replace(".0", "")
sheet3.cell(4, 13).value = f"{txt_bianhao}({len(bianhao)}处)"
#这里为了测试,先避开不同壁厚的
for i in range(0, min(len(unique_result_list ),6)):
sheet3.cell((7 + i), 1).value = unique_result_list[i][0]
sheet3.cell((7 + i), 3).value = unique_result_list[i][1]
# 这里开始填写目录
workbook_new["焊接工艺目录"].cell((7 + count_sheet), 1).value = sheet3.cell(4, 13).value
workbook_new["焊接工艺目录"].cell((7 + count_sheet), 2).value = sheet3.cell(4, 4).value
workbook_new["焊接工艺目录"].cell((7 + count_sheet), 3).value = sheet3.cell(2, 14).value
# 提取第1列的第8行到11行的数据
data_weld = []
for row in range(24,27):
cell_value = sheet3.cell(row=row, column=1).value
if cell_value is not None:
data_weld.append(cell_value)
# 去重并去除空值
unique_data = list(set(filter(None, data_weld)))
# 将数据转换为字符串并用+号连接
result = '+'.join(map(str, unique_data))
workbook_new["焊接工艺目录"].cell((7 + count_sheet), 4).value = result
workbook_new["焊接工艺目录"].cell((7 + count_sheet), 5).value = sheet3.cell(4, 8).value
workbook_new["焊接工艺目录"].cell((7 + count_sheet), 6).value = sheet3.cell(21, 8).value
count_sheet += 1
workbook_new.save(new_file_path)
print(f"已创建并保存文件:{new_file_path}")
"""
新增图形界面
"""
root = tk.Tk()
root.withdraw() # 隐藏主窗口
input_file = select_input_file()
if not input_file:
print("未选择输入文件")
exit()
template_file = select_template_file()
if not template_file:
print("未选择模板文件")
exit()
target_folder = select_target_folder()
if not target_folder:
print("未选择输出文件夹")
exit()
# 读取原始数据文件
df_merge = pd.read_excel(input_file)
# 将列“焊缝类型”数值为“对焊”,“承插焊”,“开口焊”的行里列“主管外径”,“壁厚”类型改为浮点型
selected_weld_types = ['对焊', '承插焊', '开口焊']
df_merge.loc[df_merge['焊接类型'].isin(selected_weld_types), ['主管外径', '壁厚']] = df_merge.loc[df_merge['焊接类型'].isin(selected_weld_types), ['主管外径', '壁厚']].astype(float)
# 将列“焊缝类型”数值为"开口焊"的行里列“支管外径”,“支管壁厚”类型改为浮点型
df_merge.loc[df_merge['焊接类型'] == '开口焊', ['支管外径', '支管壁厚']] = df_merge.loc[df_merge['焊接类型'] == '开口焊', ['支管外径', '支管壁厚']].astype(float)
df_merge = process01_df(df_merge)
df_merge['工艺卡编号'] = df_merge.apply(fill_process_card, axis=1)
df_merge = df_merge[df_merge["工艺卡编号"] != ""]
# 对相同的“管线号”,“焊缝类型”,“焊缝材质”进行分组,并对“焊缝数量”求和
#df_merge = df_merge.groupby(["管线号", "焊接类型", "工艺卡编号", "焊接材质", "外径壁厚", "管线寸口"], as_index=False).agg({"焊接寸口": "sum"})
# df_merge = df_merge.groupby(["管线号", "焊接类型", "工艺卡编号", "材质", "外径壁厚", "管线寸口"], as_index=False).agg({"道数": "sum","焊接寸口": "sum"})
# 根据“管线号”,“焊缝类型”,“焊缝材质”进行排序
# df_merge.sort_values(by=["管线号", "材质", "管线寸口", "外径壁厚", "焊接类型"], inplace=True)
# 获取所有不同的管线号
pipeline_ids = df_merge["管线号"].unique() # 请确保替换为实际的列名
output_file = f"{target_folder}/new.xlsx"
df_merge.to_excel(output_file, index=False)
# 确保目标文件夹存在
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# 遍历所有管线号,复制模板文件并重命名
for pipeline_number in pipeline_ids:
copy_template_to_new_file(pipeline_number, template_file, target_folder)