|

楼主 |
发表于 2024-7-29 13:27:40
|
显示全部楼层
- import pandas as pd
- import openpyxl
- from openpyxl import load_workbook
- from openpyxl.drawing.image import Image
- import os
- import shutil
- 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)
- # 复制模板文件到新文件路径
- 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)
- # 遍历所有sheet名,填充数据
- sheet_names = workbook_new.sheetnames
- for sheet_name in sheet_names:
- filtered_rows_od = df_merge[df_merge["管线号"] == pipeline_number_copy]
- list_od = filtered_rows_od['外径壁厚'].unique()
- # 填充数据到C8单元格开始的位置,向下填充,左对齐
- start_row = 8
- start_col = 3
- for index, value in enumerate(list_od):
- cell = sheet_name.cell(row=start_row + index, column=start_col, value=value)
- cell.alignment = openpyxl.styles.Alignment(horizontal='left')
- # 保存修改后的Excel文件
- workbook_new.save(new_file_path)
- print(f"已创建并保存文件:{new_file_path}")
- # 读取原始数据文件
- df_merge = pd.read_excel("提取数据.xlsx")
- # 获取所有不同的管线号
- pipeline_ids = df_merge["管线号"].unique() # 请确保替换为实际的列名
- # 定义模板文件路径和目标文件夹路径
- template_file = "工艺卡模板.xlsx"
- target_folder = "C:\\测试文件夹"
- # 确保目标文件夹存在
- 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("所有管线号对应的文件已成功创建!")
复制代码
错误如下
D:\PYTHON_NEW\pythonProject\.venv\Scripts\python.exe "D:\PYTHON_NEW\pythonProject\提取python excel\11 测试生成管线对应的工艺卡的填充数据.py"
Traceback (most recent call last):
File "D:\PYTHON_NEW\pythonProject\提取python excel\11 测试生成管线对应的工艺卡的填充数据.py", line 89, in <module>
copy_template_to_new_file(pipeline_number, template_file, target_folder)
File "D:\PYTHON_NEW\pythonProject\提取python excel\11 测试生成管线对应的工艺卡的填充数据.py", line 62, in copy_template_to_new_file
cell = sheet_name.cell(row=start_row + index, column=start_col, value=value)
^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'cell'
Process finished with exit code 1
ai老师帮分析下呢 |
|