kinkiSundry / app.py
taskforce-dev's picture
Update app.py
002ca69 verified
raw
history blame
3.24 kB
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 = ['伝票連番', '連番行番号', '得意先コード', '伝票区分', '商品コード', '商品名区分',
'売上数量_在庫用数量', '売上金額', '形番号', '郵便番号',
'住所1', '住所2', '電話番号', '受注_図面番号', '受注_サイズ']
df.drop(columns_to_drop, axis=1, inplace=True, errors='ignore')
# Rename specified columns
column_name_mapping = {
'伝票日付': '請求日',
'伝票番号': '請求番号',
'行番号': 'ユーザ定義項目1',
'商品名': '品名',
'売上数量_請求用数量': '数量',
'売上単価': '単価',
'得意先名': 'タグ',
'行摘要': '得意先名',
'ユーザーNO1': '明細ユーザ定義項目1',
'受注_備考': '備考'
}
df.rename(columns=column_name_mapping, inplace=True)
# Convert the '請求日' column from YYYYMMDD to YYYY/MM/DD format
df['請求日'] = df['請求日'].astype(str).apply(lambda x: x[:4] + '/' + x[4:6] + '/' + x[6:8])
df['明細ユーザ定義項目1'] = df['請求日']
df['ユーザ定義項目1'] = df['請求日']
# 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('単位')] = ''
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)
# Save the modified dataframe to the output file
output_file_path = "processed.csv"
df_modified.to_csv(output_file_path, index=False)
return output_file_path
# Save the modified dataframe to the output file
output_file_path = "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(share = True)