file_column上传大写文件时出错的解决办法

本文介绍了一个关于文件移动路径比较大小写敏感的问题,并提供了解决方案。通过修改文件移动的条件判断,确保无论文件路径的大小写如何都能正确移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决办法:

vendor/plugins/file-column-0.3.1/lib下file_column.rb文件

里的

#FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path == local_file_path
 #modified by victor;
 FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path.downcase == local_file_path.downcase

 

改成如上即可

---------------黑山 
import pandas as pd import re from datetime import datetime def parse_log_to_excel(log_file_path, excel_file_path): """ 解析日志文件并转换为Excel表格 :param log_file_path: 日志文件路径 :param excel_file_path: 输出的Excel文件路径 """ # 正则表达式匹配日志格式 pattern = re.compile( r'\[(.*?)\].*?Bi Station:\s*(\w+).*?Position:\s*(\w+).*?Status:\s*(\w+)' ) data = [] with open(log_file_path, 'r', encoding='utf-8') as file: for line in file: match = pattern.search(line) if match: timestamp, bi_station, position, status = match.groups() # 转换间格式 try: dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') except ValueError: dt = timestamp # 保持原始格式若转换失败 data.append({ 'Timestamp': dt, 'Bi Station': bi_station, 'Position': position, 'Status': status, 'Raw Log': line.strip() }) # 创建DataFrame并按序排序 df = pd.DataFrame(data) df.sort_values(by='Timestamp', inplace=True) # 写入Excel文件 with pd.ExcelWriter(excel_file_path, engine='openpyxl') as writer: df.to_excel(writer, index=False, sheet_name='Test Results') # 获取工作表对象设置列宽 worksheet = writer.sheets['Test Results'] worksheet.column_dimensions['A'].width = 20 # 间列 worksheet.column_dimensions['B'].width = 15 # Bi Station worksheet.column_dimensions['C'].width = 15 # Position worksheet.column_dimensions['D'].width = 10 # Status worksheet.column_dimensions['E'].width = 80 # 原始日志 print(f"成功转换 {len(df)} 条记录到 {excel_file_path}") # 示例使用 if __name__ == "__main__": parse_log_to_excel('test_logs.txt', 'test_results.xlsx')运行上述代码出现Traceback (most recent call last): File "d:\python_project\789.py", line 56, in <module> parse_log_to_excel('D:\python学习\Execel输出\log_example.txt', 'D:\python学习\Execel输出\log_example.xlsx') File "d:\python_project\789.py", line 38, in parse_log_to_excel df.sort_values(by='Timestamp', inplace=True) File "C:\Users\c60060592\Miniconda3\envs\py310\lib\site-packages\pandas\core\frame.py", line 7189, in sort_values k = self._get_label_or_level_values(by[0], axis=axis) File "C:\Users\c60060592\Miniconda3\envs\py310\lib\site-packages\pandas\core\generic.py", line 1911, in _get_label_or_level_values raise KeyError(key) KeyError: 'Timestamp'如何解决
06-12
import openpyxl import os from openpyxl.utils import get_column_letter from openpyxl.styles import PatternFill def preprocess_classification_file(class_file_path): """ 预处理分类文件,构建映射字典 :param class_file_path: 分类文件路径 :return: 映射字典 {三级分类: {四级分类: (二级分类, 四级编码)}} """ try: wb = openpyxl.load_workbook(class_file_path) ws = wb["工作职责分类、岗位说明书分类(20250730客户确认版)"] mapping = {} current_l2 = None current_l3 = None # 从第3行开始读取数据(跳过表头) for row in range(3, ws.max_row + 1): l2_code = ws.cell(row, 3).value # C列:二级分类编码 l2_name = ws.cell(row, 4).value # D列:二级分类名称 l3_code = ws.cell(row, 5).value # E列:三级分类编码 l3_name = ws.cell(row, 6).value # F列:三级分类名称 l4_code = ws.cell(row, 7).value # G列:四级分类编码 l4_name = ws.cell(row, 8).value # H列:四级分类名称 # 更新当前分类(处理合并单元格) if l2_name: current_l2 = l2_name if l3_name: current_l3 = l3_name # 只处理有四级分类的行 if l4_name and current_l3 and current_l2: if current_l3 not in mapping: mapping[current_l3] = {} mapping[current_l3][l4_name] = (current_l2, l4_code) print(mapping) return mapping except Exception as e: print(f"处理分类文件出错: {str(e)}") return {} def process_all_files(root_source_folder, template_file, class_file_path,root_output_folder): """ 处理根目录下所有子文件夹中的Excel文件并生成总表 :param root_source_folder: 源文件根目录路径 :param template_file: HRHB模板文件路径 :param root_output_folder: 输出根目录路径 """ # 预处理分类文件(只处理一次) class_mapping = preprocess_classification_file(class_file_path) # 创建汇总工作簿 summary_wb = openpyxl.Workbook() summary_ws = summary_wb.active summary_ws.title = "汇总表" # 设置汇总表表头 headers = ["文件名", "所属部门(文件名)","工作职责", "工作内容描述", "工作职责分类",'所属部门(表格)'] for col, header in enumerate(headers, start=1): summary_ws.cell(row=1, column=col, value=header) row_counter = 2 # 从第2行开始填充数据 # 遍历根目录下的所有子文件夹 for foldername, subfolders, filenames in os.walk(root_source_folder): # 获取当前文件夹的直接父文件夹名称 current_parent_folder = os.path.basename(foldername) # 找到第一个数字后的点号位置 dot_pos = current_parent_folder.find('.') + 1 # +1 跳过点号本身 # 找到"岗位说明书"的起始位置 title_pos = current_parent_folder.find('岗位说明书') # 切片提取中间部分 pf = current_parent_folder[dot_pos:title_pos].strip() # strip() 去除首尾空格 for filename in filenames: if filename.endswith('.xlsx') and not filename.startswith('~$'): try: source_file = os.path.join(foldername, filename) relative_path = os.path.relpath(foldername, root_source_folder) output_folder = os.path.join(root_output_folder, relative_path) # 处理单个文件并获取生成的数据 generated_data = generate_hrhb_job_duties( source_file, template_file, output_folder, class_mapping ) # 将数据添加到汇总表 if generated_data: for duty in generated_data: summary_ws.cell(row=row_counter, column=1, value=filename) summary_ws.cell(row=row_counter, column=2, value=pf) summary_ws.cell(row=row_counter, column=3, value=duty["work_area"]) summary_ws.cell(row=row_counter, column=4, value=duty["duty_desc"]) summary_ws.cell(row=row_counter, column=5, value=duty["department"]) summary_ws.cell(row=row_counter, column=6, value=duty["department2"]) summary_ws.cell(row=row_counter, column=7, value=duty["position_class"]) summary_ws.cell(row=row_counter, column=8, value=duty["duty_class_code"]) row_counter += 1 except Exception as e: print(f"处理文件 {filename} 出错: {str(e)}") # 保存汇总表 summary_file = os.path.join(root_output_folder, "HRHB_工作职责汇总表.xlsx") summary_wb.save(summary_file) print(f"成功生成汇总表: {summary_file}") def generate_hrhb_job_duties(source_file, template_file, output_folder, class_mapping): """处理单个文件并返回提取的数据""" try: source_wb = openpyxl.load_workbook(source_file) source_ws = source_wb["岗位职责"] department = source_ws["B1"].value department2 = source_ws["D1"].value duties_data = [] # 动态检测职责结束位置 row = 4 while True: importance = source_ws[f"A{row}"].value work_area = source_ws[f"B{row}"].value duty_desc = source_ws[f"C{row}"].value # 检测数据结束:当连续3行无数据停止 if not (importance or work_area or duty_desc): # 检查后续行确认是否真正结束 next_rows_empty = True for i in range(1, 4): # 检查后续3行 next_imp = source_ws[f"A{row + i}"].value next_area = source_ws[f"B{row + i}"].value next_desc = source_ws[f"C{row + i}"].value if next_imp or next_area or next_desc: next_rows_empty = False break if next_rows_empty: break # 收集有效数据 if all([importance, work_area, duty_desc]): # 匹配分类信息 position_class = "未知" duty_class_code = "未知" if department2 in class_mapping: dept_mapping = class_mapping[department2] if work_area in dept_mapping: position_class, duty_class_code = dept_mapping[work_area] duties_data.append({ "importance": importance, "work_area": work_area, "duty_desc": duty_desc, "department": department, "department2": department2, "position_class": position_class, # 新增字段 "duty_class_code": duty_class_code # 新增字段 }) row += 1 if not duties_data: print(f"警告: 在 {source_file} 中未找到有效的工作职责数据") return None template_wb = openpyxl.load_workbook(template_file) template_ws = template_wb["基本工作职责"] # 清空模板数据 for row in range(2, template_ws.max_row + 1): for col in range(1, 8): # 现在有7列 template_ws.cell(row=row, column=col).value = None # 添加新增列的标题(如果不存在) if template_ws["F1"].value is None: template_ws["F1"] = "岗位分类" template_ws["G1"] = "工作职责分类编码" # 设置标题样式(黄色背景) yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") for col in ["F1", "G1"]: template_ws[col].fill = yellow_fill start_row = 2 for idx, duty in enumerate(duties_data, start=start_row): template_ws[f"A{idx}"] = duty["work_area"] template_ws[f"B{idx}"] = duty["duty_desc"] template_ws[f"C{idx}"] = duty["department"] template_ws[f"D{idx}"] = "江门市烟草分公司(本级)" template_ws[f"E{idx}"] = duty["department2"] template_ws[f"F{idx}"] = duty["position_class"] # 新增字段 template_ws[f"G{idx}"] = duty["duty_class_code"] # 新增字段 # 调整列宽(现在有7列) for col in range(1, 8): max_length = 0 column = get_column_letter(col) for cell in template_ws[column]: if cell.value: max_length = max(max_length, len(str(cell.value))) template_ws.column_dimensions[column].width = (max_length + 4) * 1.2 os.makedirs(output_folder, exist_ok=True) source_name = os.path.splitext(os.path.basename(source_file))[0] output_file = os.path.join(output_folder, f"HRHB_{source_name}_已填充.xlsx") template_wb.save(output_file) print(f"成功生成文件: {output_file}") return duties_data except Exception as e: print(f"处理文件出错: {str(e)}") return None 并没有匹配,全都是未知
最新发布
08-01
class FileMergeApp(ttk.Frame): """文件合并统计分析界面""" def __init__(self, master): super().__init__(master) self.pack(fill=tk.BOTH, expand=True) # 创建标题 title_label = tk.Label( self, text="Excel文件合并统计分析", font=("黑体", 16, "bold"), fg="#2c3e50" ) title_label.pack(pady=20) # 创建说明标签 desc_label = tk.Label( self, text="此功能用于合并多个Excel文件并生成统计分析报告", font=("宋体", 12), fg="#7f8c8d" ) desc_label.pack(pady=10) # 创建功能容器 container = ttk.Frame(self) container.pack(fill=tk.BOTH, expand=True, padx=50, pady=20) container.columnconfigure(0, weight=1) container.rowconfigure((0, 1, 2, 3), weight=1) # 添加文件选择区域 file_frame = ttk.LabelFrame(container, text="选择Excel文件") file_frame.grid(row=0, column=0, sticky="ew", pady=10) self.file_listbox = tk.Listbox(file_frame, height=6) self.file_listbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=5) btn_frame = ttk.Frame(file_frame) btn_frame.pack(fill=tk.X, padx=10, pady=5) add_btn = ttk.Button(btn_frame, text="添加文件", command=self.add_files) add_btn.pack(side=tk.LEFT, padx=5) remove_btn = ttk.Button(btn_frame, text="移除选中", command=self.remove_file) remove_btn.pack(side=tk.LEFT, padx=5) clear_btn = ttk.Button(btn_frame, text="清空列表", command=self.clear_files) clear_btn.pack(side=tk.RIGHT, padx=5) # 添加合并选项 options_frame = ttk.LabelFrame(container, text="合并选项") options_frame.grid(row=1, column=0, sticky="ew", pady=10) self.header_var = tk.BooleanVar(value=True) header_cb = ttk.Checkbutton( options_frame, text="包含标题行", variable=self.header_var ) header_cb.grid(row=0, column=0, padx=10, pady=5, sticky="w") self.merge_var = tk.StringVar(value="append") ttk.Radiobutton( options_frame, text="追加数据", variable=self.merge_var, value="append" ).grid(row=0, column=1, padx=10, pady=5, sticky="w") ttk.Radiobutton( options_frame, text="按列合并", variable=self.merge_var, value="columns" ).grid(row=0, column=2, padx=10, pady=5, sticky="w") # 添加统计选项 stats_frame = ttk.LabelFrame(container, text="统计分析") stats_frame.grid(row=2, column=0, sticky="ew", pady=10) self.stats_vars = { "summary": tk.BooleanVar(value=True), "count": tk.BooleanVar(value=True), "avg": tk.BooleanVar(value=True), "minmax": tk.BooleanVar(value=True) } ttk.Checkbutton( stats_frame, text="生成汇总统计", variable=self.stats_vars["summary"] ).grid(row=0, column=0, padx=10, pady=5, sticky="w") ttk.Checkbutton( stats_frame, text="计数统计", variable=self.stats_vars["count"] ).grid(row=0, column=1, padx=10, pady=5, sticky="w") ttk.Checkbutton( stats_frame, text="平均值计算", variable=self.stats_vars["avg"] ).grid(row=0, column=2, padx=10, pady=5, sticky="w") ttk.Checkbutton( stats_frame, text="极值分析", variable=self.stats_vars["minmax"] ).grid(row=0, column=3, padx=10, pady=5, sticky="w") # 添加执行按钮 execute_btn = ttk.Button( container, text="开始合并与分析", command=self.start_merge, style="Accent.TButton" ) execute_btn.grid(row=3, column=0, pady=20) # 配置样式 self.style = ttk.Style() self.style.configure("Accent.TButton", foreground="white", background="#3498db", font=("Arial", 10, "bold"), padding=6) def add_files(self): files = filedialog.askopenfilenames( filetypes=[("Excel文件", "*.xls *.xlsx")] ) for file in files: if file not in self.file_listbox.get(0, tk.END): self.file_listbox.insert(tk.END, file) def remove_file(self): selected = self.file_listbox.curselection() if selected: self.file_listbox.delete(selected[0]) def clear_files(self): self.file_listbox.delete(0, tk.END) def start_merge(self): if self.file_listbox.size() == 0: messagebox.showwarning("警告", "请至少添加一个Excel文件") return # 模拟处理过程 messagebox.showinfo("提示", "文件合并与分析功能正在开发中...") 功能界面:文件合并统计分析界面 (FileMergeApp) 文件列表:添加、移除和清空Excel文件 合并选项:指定列不合并:[]、工作页合并、区块合并、按列合并。指定列不合并:[]输入指定列A/B/C/D...数据不参与合并,工作页合并:将每个Excel文件工作页合并到同一个文件不同工作页中,区块合并:将每个Excel文件工作页当前数据行合并到同一个文件同一个工作页中,按列合并:合并列对应的合并单元格数据进行判断?若为数值则进行相加,若为字符串则先去重,不同字符串以'、'间隔并排合并在一个单元格 统计分析选项:汇总统计、计数统计 执行按钮:开始合并与分析。开始合并与分析:点击执行以上功能,另存生成一个(*.xls)和(*.xlsx)后缀的Excel文件文件弹出一个文本浏览框,可手动选择该Excel新文件保存的路径位置和文件名。该文件生成后自动在其文件名后缀加上当前生成间戳。
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值