import pandas as pd import gradio as gr # 関数を定義 def process_csv(input_file): # Load the CSV file with MS932 encoding df = pd.read_csv(input_file.name, encoding="MS932", header=0) # Insert '行形式' column twice at the beginning for _ in range(2): if '行形式' not in df.columns: df.insert(0, '行形式', '') # Drop specified columns columns_to_drop = ['伝票連番', '連番行番号', '行番号', '伝票区分', '商品コード', '商品名区分', '売上数量_在庫用数量', '売上金額', '形番号', 'ユーザーNO1', '得意先名', '郵便番号', '住所1', '住所2', '電話番号', '受注_図面番号', '受注_サイズ'] df.drop(columns_to_drop, axis=1, inplace=True, errors='ignore') # Rename specified columns column_name_mapping = { '伝票日付': '請求日', '伝票番号': '請求番号', '商品名': '品名', '売上数量_請求用数量': '数量', '売上単価': '単価', '行摘要': '件名', '受注_備考': 'メモ' } df.rename(columns=column_name_mapping, inplace=True) # Group by '請求番号' and modify rows df_grouped = df.groupby('請求番号') dfs = [] for _, group in df_grouped: header_row = group.iloc[0].copy() new_group = pd.concat([header_row.to_frame().T, group]) new_group.iloc[0, new_group.columns.get_loc('行形式')] = 'ヘッダ' new_group.iloc[1:, new_group.columns.get_loc('行形式')] = '明細' new_group.iloc[0, new_group.columns.get_loc('品名')] = '' new_group.iloc[0, new_group.columns.get_loc('数量')] = '' new_group.iloc[0, new_group.columns.get_loc('単価')] = '' new_group.iloc[0, new_group.columns.get_loc('単位')] = '' clear_cols = ['得意先コード', '請求日', '請求番号', '件名', 'メモ'] for col in clear_cols: if col in new_group.columns: new_group.iloc[1:, new_group.columns.get_loc(col)] = '' dfs.append(new_group) df_modified = pd.concat(dfs) # Save the modified dataframe to the output file output_file_path = "/mnt/data/processed.csv" df_modified.to_csv(output_file_path, index=False, encoding="MS932") return output_file_path # Webアプリを作成 app = gr.Interface(fn=process_csv, inputs="file", outputs="file") # Webアプリを起動 app.launch(server_port=8080)