Spaces:
Sleeping
Sleeping
| 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, dtype={"請求先コード": str}, encoding="MS932", header=0) | |
| #伝票連番が空白の行を削除する | |
| df = df.dropna(subset=["伝票連番"]) | |
| #現金入金額に全ての入金額を寄せる | |
| #請求先コードが空白でない行をフィルタリング | |
| non_empty_rows = df['請求先コード'].notna() | |
| #現金入金、手形入金、値引入金の合計を計算 | |
| df.loc[non_empty_rows, 'その他入金'] = df.loc[non_empty_rows, ['現金入金', '手形入金', '値引入金', 'その他入金']].sum(axis=1) | |
| # Drop specified columns | |
| columns_to_drop = [ | |
| '郵便番号', | |
| '住所1', | |
| '住所2', | |
| '敬称区分', | |
| '請求開始残高', | |
| '回収予定日', | |
| '回収区分', | |
| '現金入金', | |
| '手形入金', | |
| '値引入金', | |
| '繰越金額', | |
| '掛売上額', | |
| '掛返品額', | |
| '掛売値引', | |
| '掛売消費税', | |
| '今回請求金額', | |
| '最終請求書発行日', | |
| '請求番号', | |
| '担当者コード', | |
| '担当者名', | |
| '部署名', | |
| '伝票枚数', | |
| '得意先名カナ', | |
| '伝票連番', | |
| '行番号', | |
| '伝票区分', | |
| '商品コード', | |
| '図面番号', | |
| 'サイズ', | |
| '金額', | |
| '消費税', | |
| '消費税区分', | |
| '消費税率', | |
| '手形番号', | |
| '振出日', | |
| '手形期日', | |
| '納品先名', | |
| '税区分', | |
| '税端数区分' | |
| ] | |
| df.drop(columns_to_drop, axis=1, inplace=True, errors='ignore') | |
| # Rename specified columns | |
| column_name_mapping = { | |
| '請求先コード': '得意先コード', | |
| '前回請求金額': '前回請求残高', | |
| 'その他入金': '今回入金額', | |
| '伝票日付': '明細ユーザ定義項目1', | |
| '伝票番号': '明細ユーザ定義項目2', | |
| '商品名': '品名', | |
| '行摘要': '明細ユーザ定義項目3', | |
| '商品名': '品名' | |
| } | |
| df.rename(columns=column_name_mapping, inplace=True) | |
| # Convert the '請求日' column from YYYYMMDD to YYYY/MM/DD format | |
| df.loc[df['請求日'].notna() & (df['請求日'] != ''), '請求日'] = df['請求日'].astype(str).apply(lambda x: x[:4] + '/' + x[4:6] + '/' + x[6:8]) | |
| # Identify rows where '得意先コード' has a value (i.e., is not NaN) | |
| rows_with_values = df['得意先コード'].notna() | |
| # Iterate over the rows in reverse to avoid shifting index issues | |
| for idx in range(len(df) - 1, -1, -1): | |
| if rows_with_values.iloc[idx]: | |
| # Insert a new row above the current row | |
| df = pd.concat([ | |
| df.iloc[:idx], | |
| pd.DataFrame([df.iloc[idx]], columns=df.columns), | |
| df.iloc[idx:] | |
| ]).reset_index(drop=True) | |
| # Move the specified columns from the lower row to the inserted row | |
| columns_to_move = ['得意先コード', '得意先名', '請求日', '前回請求残高', '今回入金額'] | |
| df.loc[idx, columns_to_move] = df.loc[idx + 1, columns_to_move] | |
| df.loc[idx + 1, columns_to_move] = [float('nan')] * len(columns_to_move) | |
| # Identify rows where '得意先コード' has a value and where the subsequent row does not | |
| is_inserted_row = df['得意先コード'].notna() & df['得意先コード'].shift(-1).isna() | |
| # Remove specified columns from the identified rows | |
| columns_to_remove = ['明細ユーザ定義項目1', '明細ユーザ定義項目2', '品名', '単位', '数量', '単価', '明細ユーザ定義項目3'] | |
| df.loc[is_inserted_row, columns_to_remove] = float('nan') | |
| # Add a new column named "行形式" | |
| df.insert(0, "行形式", "") | |
| # Set the value "ヘッダ" for rows where "請求先コード" is not empty | |
| df.loc[df["得意先コード"].notna(), "行形式"] = "ヘッダ" | |
| # Set the value "明細" for rows where "請求先コード" is empty | |
| df.loc[df["得意先コード"].isna(), "行形式"] = "明細" | |
| # '得意先コード'列を整数型に変換 | |
| df.loc[df['得意先コード'].notna() & (df['得意先コード'] != ''), '得意先コード'] = df.loc[df['得意先コード'].notna() & (df['得意先コード'] != ''), '得意先コード'].astype(int) | |
| # Save the modified dataframe to the output file | |
| output_file_path = 'processed_file.csv' | |
| df.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(share=True) | |