Spaces:
Sleeping
Sleeping
Commit ·
0b0436e
1
Parent(s): f2344fb
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def process_csv(input_file):
|
| 5 |
+
|
| 6 |
+
# Load the CSV file with MS932 encoding
|
| 7 |
+
df = pd.read_csv(input_file.name, encoding="UTF-8", header=0)
|
| 8 |
+
|
| 9 |
+
#伝票連番が空白の行を削除する
|
| 10 |
+
df = df.dropna(subset=["伝票連番"])
|
| 11 |
+
|
| 12 |
+
#現金入金額に全ての入金額を寄せる
|
| 13 |
+
#請求先コードが空白でない行をフィルタリング
|
| 14 |
+
non_empty_rows = df['請求先コード'].notna()
|
| 15 |
+
|
| 16 |
+
#現金入金、手形入金、値引入金の合計を計算
|
| 17 |
+
df.loc[non_empty_rows, 'その他入金'] = df.loc[non_empty_rows, ['現金入金', '手形入金', '値引入金', 'その他入金']].sum(axis=1)
|
| 18 |
+
|
| 19 |
+
# Drop specified columns
|
| 20 |
+
columns_to_drop = [
|
| 21 |
+
'郵便番号',
|
| 22 |
+
'住所1',
|
| 23 |
+
'住所2',
|
| 24 |
+
'敬称区分',
|
| 25 |
+
'請求開始残高',
|
| 26 |
+
'回収予定日',
|
| 27 |
+
'回収区分',
|
| 28 |
+
'現金入金',
|
| 29 |
+
'手形入金',
|
| 30 |
+
'値引入金',
|
| 31 |
+
'繰越金額',
|
| 32 |
+
'掛売上額',
|
| 33 |
+
'掛返品額',
|
| 34 |
+
'掛売値引',
|
| 35 |
+
'掛売消費税',
|
| 36 |
+
'今回請求金額',
|
| 37 |
+
'最終請求書発行日',
|
| 38 |
+
'請求番号',
|
| 39 |
+
'担当者コード',
|
| 40 |
+
'担当者名',
|
| 41 |
+
'部署名',
|
| 42 |
+
'伝票枚数',
|
| 43 |
+
'得意先名カナ',
|
| 44 |
+
'伝票連番',
|
| 45 |
+
'行番号',
|
| 46 |
+
'伝票区分',
|
| 47 |
+
'商品コード',
|
| 48 |
+
'図面番号',
|
| 49 |
+
'サイズ',
|
| 50 |
+
'金額',
|
| 51 |
+
'消費税',
|
| 52 |
+
'消費税区分',
|
| 53 |
+
'消費税率',
|
| 54 |
+
'手形番号',
|
| 55 |
+
'振出日',
|
| 56 |
+
'手形期日',
|
| 57 |
+
'納品先名',
|
| 58 |
+
'税区分',
|
| 59 |
+
'税端数区分'
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
df.drop(columns_to_drop, axis=1, inplace=True, errors='ignore')
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Rename specified columns
|
| 66 |
+
column_name_mapping = {
|
| 67 |
+
'請求先コード': '得意先コード',
|
| 68 |
+
'前回請求金額': '前回請求残高',
|
| 69 |
+
'その他入金': '今回入金額',
|
| 70 |
+
'伝票日付': '明細ユーザ定義項目1',
|
| 71 |
+
'伝票番号': '明細ユーザ定義項目2',
|
| 72 |
+
'商品名': '品名',
|
| 73 |
+
'行摘要': '明細ユーザ定義項目3',
|
| 74 |
+
'商品名': '品名'
|
| 75 |
+
}
|
| 76 |
+
df.rename(columns=column_name_mapping, inplace=True)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Convert the '請求日' column from YYYYMMDD to YYYY/MM/DD format
|
| 80 |
+
df.loc[df['請求日'].notna() & (df['請求日'] != ''), '請求日'] = df['請求日'].astype(str).apply(lambda x: x[:4] + '/' + x[4:6] + '/' + x[6:8])
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Identify rows where '得意先コード' has a value (i.e., is not NaN)
|
| 84 |
+
rows_with_values = df['得意先コード'].notna()
|
| 85 |
+
|
| 86 |
+
# Iterate over the rows in reverse to avoid shifting index issues
|
| 87 |
+
for idx in range(len(df) - 1, -1, -1):
|
| 88 |
+
if rows_with_values.iloc[idx]:
|
| 89 |
+
# Insert a new row above the current row
|
| 90 |
+
df = pd.concat([
|
| 91 |
+
df.iloc[:idx],
|
| 92 |
+
pd.DataFrame([df.iloc[idx]], columns=df.columns),
|
| 93 |
+
df.iloc[idx:]
|
| 94 |
+
]).reset_index(drop=True)
|
| 95 |
+
|
| 96 |
+
# Move the specified columns from the lower row to the inserted row
|
| 97 |
+
columns_to_move = ['得意先コード', '得意先名', '請求日', '前回請求残高', '今回入金額']
|
| 98 |
+
df.loc[idx, columns_to_move] = df.loc[idx + 1, columns_to_move]
|
| 99 |
+
df.loc[idx + 1, columns_to_move] = [float('nan')] * len(columns_to_move)
|
| 100 |
+
|
| 101 |
+
# Identify rows where '得意先コード' has a value and where the subsequent row does not
|
| 102 |
+
is_inserted_row = df['得意先コード'].notna() & df['得意先コード'].shift(-1).isna()
|
| 103 |
+
|
| 104 |
+
# Remove specified columns from the identified rows
|
| 105 |
+
columns_to_remove = ['明細ユーザ定義項目1', '明細ユーザ定義項目2', '品名', '単位', '数量', '単価', '明細ユーザ定義項目3']
|
| 106 |
+
df.loc[is_inserted_row, columns_to_remove] = float('nan')
|
| 107 |
+
|
| 108 |
+
# Add a new column named "行形式"
|
| 109 |
+
df.insert(0, "行形式", "")
|
| 110 |
+
|
| 111 |
+
# Set the value "ヘッダ" for rows where "請求先コード" is not empty
|
| 112 |
+
df.loc[df["得意先コード"].notna(), "行形式"] = "ヘッダ"
|
| 113 |
+
|
| 114 |
+
# Set the value "明細" for rows where "請求先コード" is empty
|
| 115 |
+
df.loc[df["得意先コード"].isna(), "行形式"] = "明細"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# Save the modified dataframe to the output file
|
| 119 |
+
output_file_path = 'processed_file.csv'
|
| 120 |
+
df.to_csv(output_file_path, index=False)
|
| 121 |
+
|
| 122 |
+
return output_file_path
|
| 123 |
+
|
| 124 |
+
# Webアプリを作成
|
| 125 |
+
app = gr.Interface(fn=process_csv, inputs="file", outputs="file")
|
| 126 |
+
# Webアプリを起動
|
| 127 |
+
app.launch(debug=True, share=True)
|