鱼C论坛

 找回密码
 立即注册
查看: 184|回复: 3

[已解决]代码错误,大佬们看看哪里有问题呢

[复制链接]
发表于 2024-9-12 14:01:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
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 process_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)

        # 填写工艺卡编号,先判断最优先级的材质12CrMo
        if row['焊接类型'] == '对焊' and "12Cr" in row["材质"] and (row["壁厚"] > 13):
            df_in.at[index, '工艺卡编号'] = "HKB12-06"
        elif row['焊接类型'] == '对焊' and "12Cr" in row["材质"] and (row["壁厚"] > 10):
            df_in.at[index, '工艺卡编号'] = "HKB12-05"
        elif row['焊接类型'] == '对焊' and "12Cr" in row["材质"] and (row["壁厚"] > 8):
            df_in.at[index, '工艺卡编号'] = "HKB12-04"
        elif row['焊接类型'] == '对焊' and "12Cr" in row["材质"] and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKB12-03"
        elif row['焊接类型'] == '对焊' and "12Cr" in row["材质"] and (row["壁厚"] > 3.5):
            df_in.at[index, '工艺卡编号'] = "HKB12-02"
        elif row['焊接类型'] == '对焊' and "12Cr" in row["材质"] and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKB12-01"

        elif row['焊接类型'] == '承插焊' and "12Cr" in row["材质"] and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKC12-03"
        elif row['焊接类型'] == '承插焊' and "12Cr" in row["材质"] and (row["壁厚"] > 3.5):
            df_in.at[index, '工艺卡编号'] = "HKC12-02"
        elif row['焊接类型'] == '承插焊' and "12Cr" in row["材质"] and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKC12-01"

        elif row['焊接类型'] == '开口焊' and "12Cr" in row["材质"] and (row["支管壁厚"] > 5):
            df_in.at[index, '工艺卡编号'] = "HKD12-03"
        elif row['焊接类型'] == '开口焊' and "12Cr" in row["材质"] and (row["支管壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKD12-02"
        elif row['焊接类型'] == '开口焊' and "12Cr" in row["材质"] and (row["支管壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKD12-01"
        # 12CrMo的判断结束,接下来判断15CrMo
        elif row['焊接类型'] == '对焊' and "15Cr" in row["材质"] and (row["壁厚"] > 13):
            df_in.at[index, '工艺卡编号'] = "HKB15-06"
        elif row['焊接类型'] == '对焊' and "15Cr" in row["材质"] and (row["壁厚"] > 10):
            df_in.at[index, '工艺卡编号'] = "HKB15-05"
        elif row['焊接类型'] == '对焊' and "15Cr" in row["材质"] and (row["壁厚"] > 8):
            df_in.at[index, '工艺卡编号'] = "HKB15-04"
        elif row['焊接类型'] == '对焊' and "15Cr" in row["材质"] and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKB15-03"
        elif row['焊接类型'] == '对焊' and "15Cr" in row["材质"] and (row["壁厚"] > 3.5):
            df_in.at[index, '工艺卡编号'] = "HKB15-02"
        elif row['焊接类型'] == '对焊' and "15Cr" in row["材质"] and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKB15-01"

        elif row['焊接类型'] == '承插焊' and "15Cr" in row["材质"] and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKC15-03"
        elif row['焊接类型'] == '承插焊' and "15Cr" in row["材质"] and (row["壁厚"] > 3.5):
            df_in.at[index, '工艺卡编号'] = "HKC15-02"
        elif row['焊接类型'] == '承插焊' and "15Cr" in row["材质"] and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKC15-01"

        elif row['焊接类型'] == '开口焊' and "15Cr" in row["材质"] and (row["支管壁厚"] > 5):
            df_in.at[index, '工艺卡编号'] = "HKD15-03"
        elif row['焊接类型'] == '开口焊' and "15Cr" in row["材质"] and (row["支管壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKD15-02"
        elif row['焊接类型'] == '开口焊' and "15Cr" in row["材质"] and (row["支管壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKD15-01"
        # 15CrMo的判断结束,接下来判断304类
        elif row['焊接类型'] == '对焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 7):
            df_in.at[index, '工艺卡编号'] = "HKB304-04"
        elif row['焊接类型'] == '对焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 5):
            df_in.at[index, '工艺卡编号'] = "HKB304-03"
        elif row['焊接类型'] == '对焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKB304-02"
        elif row['焊接类型'] == '对焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKB304-01"

        elif row['焊接类型'] == '承插焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKC304-03"
        elif row['焊接类型'] == '承插焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKC304-02"
        elif row['焊接类型'] == '承插焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKC304-01"

        elif row['焊接类型'] == '开口焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["支管壁厚"] > 6.5):
            df_in.at[index, '工艺卡编号'] = "HKD304-04"
        elif row['焊接类型'] == '开口焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["支管壁厚"] > 5):
            df_in.at[index, '工艺卡编号'] = "HKD304-03"
        elif row['焊接类型'] == '开口焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["支管壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKD304-02"
        elif row['焊接类型'] == '开口焊' and ("304" in row["材质"] or "316" in row["材质"]) and (row["支管壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKD304-01"
        # 304类的判断结束,接下来判断20
        elif row['焊接类型'] == '对焊' and "20" in row["材质"] and (row["壁厚"] > 13):
            df_in.at[index, '工艺卡编号'] = "HKB20-06"
        elif row['焊接类型'] == '对焊' and "20" in row["材质"] and (row["壁厚"] > 10):
            df_in.at[index, '工艺卡编号'] = "HKB20-05"
        elif row['焊接类型'] == '对焊' and "20" in row["材质"] and (row["壁厚"] > 8):
            df_in.at[index, '工艺卡编号'] = "HKB20-04"
        elif row['焊接类型'] == '对焊' and "20" in row["材质"] and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKB20-03"
        elif row['焊接类型'] == '对焊' and "20" in row["材质"] and (row["壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKB20-02"
        elif row['焊接类型'] == '对焊' and "20" in row["材质"] and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKB20-01"

        elif row['焊接类型'] == '承插焊' and "20" in row["材质"] and (row["壁厚"] > 5.5):
            df_in.at[index, '工艺卡编号'] = "HKC20-03"
        elif row['焊接类型'] == '承插焊' and "20" in row["材质"] and (row["壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKC20-02"
        elif row['焊接类型'] == '承插焊' and "20" in row["材质"] and (row["壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKC20-01"

        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 8) and (row["支管壁厚"] > 10):
            df_in.at[index, '工艺卡编号'] = "HKD20-10"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 8) and (row["支管壁厚"] > 8):
            df_in.at[index, '工艺卡编号'] = "HKD20-09"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 8) and (row["支管壁厚"] > 6.5):
            df_in.at[index, '工艺卡编号'] = "HKD20-08"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 8) and (row["支管壁厚"] > 5):
            df_in.at[index, '工艺卡编号'] = "HKD20-07"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 8) and (row["支管壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKD20-06"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 8) and (row["支管壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKD20-05"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 0) and (row["支管壁厚"] > 6.5):
            df_in.at[index, '工艺卡编号'] = "HKD20-04"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 0) and (row["支管壁厚"] > 5):
            df_in.at[index, '工艺卡编号'] = "HKD20-03"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 0) and (row["支管壁厚"] > 3):
            df_in.at[index, '工艺卡编号'] = "HKD20-02"
        elif row['焊接类型'] == '开口焊' and "20" in row["材质"] and (row["壁厚"] > 0) and (row["支管壁厚"] > 0):
            df_in.at[index, '工艺卡编号'] = "HKD20-01"

    return df_in
    # if "20-GB/T8163" in row["焊缝前材质"]:
    #     return "20-GB/T8163"
    # elif "20-GB/T3087" in row["焊缝前材质"]:
    #     return "20-GB/T3087"
    # elif "20-GB53120" in row["焊缝前材质"]:
    #     return "20-GB/T53"
    # else:
    #     return None


"""
这个包暂时不写,分散在程序各处
def process_excel(input_file, template_file, target_folder):
    df = pd.read_excel(input_file)
    template_df = pd.read_excel(template_file)
    #这个在后面的程序里要做组合 output_file = f"{target_folder}/output.xlsx"
    df.to_excel(output_file, index=False)
    print(f"已将 {input_file} 的内容保存到 {output_file}")
"""
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
                             # 这里开始尝试添加数据,找到数据集合
                         #filtered_rows_od = df_merge[df_merge["管线号"] == pipeline_number_copy,df_merge["工艺卡编号"] == pipeline_number_copy]
                         #processcards = filtered_rows['工艺卡编号'].unique()
                         #ws = wb[current_sheet_name]

          # 保存修改后的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(18,9) .value = pipeline_number_copy.removeprefix(r"/")


          for sheet3 in workbook_new:
              #sheet_name.cell(8,1,"好的")
              #sheet_name.cell(9,1,"不好")
              if 'H' in sheet3.title:
                #sheet3['C8'] = "11"
                #sheet3['C9'] = "12"
                filtered_rows_OD = df_merge[(df_merge['管线号'] == pipeline_number_copy) & (df_merge['工艺卡编号'] == sheet3.title)]

                # 提取“外径壁厚”和“焊接材质”列的值并存储到列表中
                result_list = []
                for index, row in filtered_rows_OD.iterrows():
                    result_list.append((row['焊接材质'], row['外径壁厚']))
                unique_result_list = list(set(result_list))


                #这里为了测试,先避开不同壁厚的
                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), 5).value = unique_result_list[i][1]
                """这里不用了,改用字典
                outer_diameter_thickness_list = filtered_rows['外径壁厚'].tolist()
                welding_material_list = filtered_rows['焊接材质'].tolist()
                list_OD = filtered_rows_OD['外径壁厚'].tolist()
                list_caizhi = filtered_rows_OD['焊接材质'].tolist()
                for i in range(0,len(list_OD)):
                    sheet3.cell((7 + i), 1).value = list_caizhi[i]
                    sheet3.cell((7 + i), 5).value = list_OD[i]
                """
          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)
df_merge = process_df(df_merge)
# for index, row in df_merge.iterrows():
#      if row['焊接类型'] == '对焊' or row['焊接类型'] == '承插焊':
#             diam_thickness = f"Φ{row['主管外径']}*{row['壁厚']}"
#             df_merge.at[index, '外径壁厚'] = diam_thickness
#      elif row['焊接类型'] == '开口焊':
#             diam_thickness = f"Φ{row['支管外径']}*{row['支管壁厚']}-Φ{row['主管外径']}*{row['壁厚']}"
#             df_merge.at[index, '外径壁厚'] = diam_thickness

output_file = f"{target_folder}/new.xlsx"

df_merge.to_excel(output_file, index=False)
# 获取所有不同的管线号
#pipeline_ids = df_merge["管线号"].unique()   # 请确保替换为实际的列名

# 定义模板文件路径和目标文件夹路径
#template_file = "工艺卡模板.xlsx"
#target_folder = r"D:\测试python读入读出文件\输出文件夹"

"""
先注释掉,后面再用,先测试做数据增加
# 确保目标文件夹存在
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)

print("所有管线号对应的文件已成功创建!")
"""
最佳答案
2024-9-12 17:04:50
报什么错?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-9-12 14:01:35 | 显示全部楼层
你好,鱼油!你贴出的代码有几处问题,我已为你找出并标注:

1. `filedialog.askopenfilename`方法的`filetypes`参数缺少了具体文件类型。
2. `process_df`函数中的一些赋值操作不太正确。
3. 还需在`select_input_file`和`select_template_file`中的filetypes参数中增加具体文件类型。

以下是修改后的代码:
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
import shutil

def select_input_file():
    file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")], title="请选择输入数据excel,.xlsx格式")
    return file_path

def select_template_file():
    file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")], title="请选择工艺卡模版的excel,.xlsx格式")
    return file_path

def select_target_folder():
    folder_path = filedialog.askdirectory(title="请选择输出文件夹")
    return folder_path

def process_df(df_in):
    for index, row in df_in.iterrows():
        # 确认列名,假设有需要'某列名称'
        value = row['某列名称']
        
        if value == '对焊' or value == '承插焊':
            diam_thickness = f"Φ{value}*{value}"
            df_in.at[index, '某列名称'] = diam_thickness
        elif value == '开口焊':
            diam_thickness = f"Φ{value}*{value}-Φ{value}*{value}"
            df_in.at[index, '某列名称'] = diam_thickness

        # 提取前段材质和前段材质编号
        if "A105" in value:
            df_in.at[index, '某列名称'] = "A105"
            df_in.at[index, '某列编号'] = 20
        elif "20-G" in value:
            df_in.at[index, '某列名称'] = "20"
            df_in.at[index, '某列编号'] = 21
        elif "20G-" in value:
            df_in.at[index, '某列名称'] = "20G"
            df_in.at[index, '某列编号'] = 22
        elif "20Ⅱ" in value:
            df_in.at[index, '某列名称'] = "20Ⅱ"
            df_in.at[index, '某列编号'] = 23
        elif "20Ⅲ" in value:
            df_in.at[index, '某列名称'] = "20Ⅲ"
            df_in.at[index, '某列编号'] = 24
        elif "12Cr1MoVG-" in value:
            df_in.at[index, '某列名称'] = "12Cr1MoVG"
            df_in.at[index, '某列编号'] = 2
        elif "12Cr1MoVⅡ" in value:
            df_in.at[index, '某列名称'] = "12Cr1MoVⅡ"
            df_in.at[index, '某列编号'] = 3
        elif "12Cr1MoVⅢ" in value:
            df_in.at[index, '某列名称'] = "12Cr1MoVⅢ"
            df_in.at[index, '某列编号'] = 4
        elif "15CrMoG-" in value:
            df_in.at[index, '某列名称'] = "15CrMoG"
            df_in.at[index, '某列编号'] = 5
        elif "15CrMoⅡ" in value:
            df_in.at[index, '某列名称'] = "15CrMoⅡ"
            df_in.at[index, '某列编号'] = 6
        elif "15CrMoⅢ" in value:
            df_in.at[index, '某列名称'] = "15CrMoⅢ"
            df_in.at[index, '某列编号'] = 7
        elif "S30408-" in value:
            df_in.at[index, '某列名称'] = "S30408"

注意事项:
1. `df_in.at[index, '列名']` 用于指定特定单元格,确保列名正确。
2. `row` 是一个 Series,`row['某列名称']` 表示访问指定列的值,要确认列名是否正确。
3. 确认“某列名称”和“某列编号”的列名是否与实际数据表对应。

尝试修改这些问题再运行看是否成功,如果还有问题,请提供更详细的错误信息。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-9-12 17:04:50 | 显示全部楼层    本楼为最佳答案   
报什么错?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-9-14 09:06:53 | 显示全部楼层
后来自己解决了,都记不起怎么解决的了。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-22 18:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表