| import pandas as pd |
| import gradio as gr |
|
|
| |
| def process_csv(input_file): |
|
|
| |
| df = pd.read_csv(input_file.name, encoding="MS932", header=0) |
|
|
| |
| for _ in range(2): |
| if '行形式' not in df.columns: |
| df.insert(0, '行形式', '') |
|
|
| |
| columns_to_drop = ['伝票連番', '連番行番号', '得意先コード', '伝票区分', '商品コード', '商品名区分', |
| '売上数量_在庫用数量', '売上金額', '形番号', '郵便番号', |
| '住所1', '住所2', '電話番号', '受注_図面番号', '受注_サイズ'] |
| df.drop(columns_to_drop, axis=1, inplace=True, errors='ignore') |
|
|
| |
| column_name_mapping = { |
| '伝票日付': '請求日', |
| '伝票番号': '請求番号', |
| '行番号': 'ユーザ定義項目1', |
| '商品名': '品名', |
| '売上数量_請求用数量': '数量', |
| '売上単価': '単価', |
| '得意先名': 'タグ', |
| '行摘要': '得意先名', |
| 'ユーザーNO1': '明細ユーザ定義項目1', |
| '受注_備考': '備考' |
| } |
| df.rename(columns=column_name_mapping, inplace=True) |
|
|
| |
| df['請求日'] = df['請求日'].astype(str).apply(lambda x: x[:4] + '/' + x[4:6] + '/' + x[6:8]) |
| df['明細ユーザ定義項目1'] = df['請求日'] |
| df['ユーザ定義項目1'] = df['請求日'] |
|
|
|
|
|
|
| |
| 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('単位')] = '' |
| new_group.iloc[0, new_group.columns.get_loc('明細ユーザ定義項目1')] = '' |
| clear_cols = ['得意先コード', '請求日', '請求番号', 'ユーザ定義項目1', 'タグ', '得意先名', '備考'] |
| 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) |
|
|
| |
| output_file_path = "processed.csv" |
| df_modified.to_csv(output_file_path, index=False) |
|
|
| return output_file_path |
|
|
| |
| output_file_path = "processed.csv" |
| df_modified.to_csv(output_file_path, index=False, encoding="MS932") |
|
|
| return output_file_path |
|
|
| |
| app = gr.Interface(fn=process_csv, inputs="file", outputs="file") |
| |
| app.launch() |