Spaces:
Sleeping
Sleeping
File size: 4,730 Bytes
0b0436e 2ed20b3 0b0436e 651d31d aeb87fc 651d31d 0b0436e 3ae88eb 0b0436e edcc0a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 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)
|