tskfkinki commited on
Commit
575f4a9
·
verified ·
1 Parent(s): bf28090

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+
4
+ # 関数を定義
5
+ def process_csv(input_file):
6
+
7
+ # Load the CSV file with MS932 encoding
8
+ df = pd.read_csv(input_file.name, encoding="MS932", header=0)
9
+
10
+ # Insert '行形式' column twice at the beginning
11
+ for _ in range(2):
12
+ if '行形式' not in df.columns:
13
+ df.insert(0, '行形式', '')
14
+
15
+ # Drop specified columns
16
+ columns_to_drop = ['伝票連番', '連番行番号', '得意先コード', '伝票区分', '商品コード', '商品名区分',
17
+ '売上数量_在庫用数量', '売上金額', '形番号', '郵便番号',
18
+ '住所1', '住所2', '電話番号', '受注_図面番号', '受注_サイズ']
19
+ df.drop(columns_to_drop, axis=1, inplace=True, errors='ignore')
20
+
21
+ # Rename specified columns
22
+ column_name_mapping = {
23
+ '伝票日付': '請求日',
24
+ '伝票番号': '請求番号',
25
+ '行番号': 'ユーザ定義項目1',
26
+ '商品名': '品名',
27
+ '売上数量_請求用数量': '数量',
28
+ '売上単価': '単価',
29
+ '得意先名': 'タグ',
30
+ '行摘要': '得意先名',
31
+ 'ユーザーNO1': '明細ユーザ定義項目1',
32
+ '受注_備考': '備考'
33
+ }
34
+ df.rename(columns=column_name_mapping, inplace=True)
35
+
36
+ # Convert the '請求日' column from YYYYMMDD to YYYY/MM/DD format
37
+ df['請求日'] = df['請求日'].astype(str).apply(lambda x: x[:4] + '/' + x[4:6] + '/' + x[6:8])
38
+ df['明細ユーザ定義項目1'] = df['請求日']
39
+ df['ユーザ定義項目1'] = df['請求日']
40
+
41
+
42
+
43
+ # Group by '請求番号' and modify rows
44
+ df_grouped = df.groupby('請求番号')
45
+ dfs = []
46
+
47
+ for _, group in df_grouped:
48
+ header_row = group.iloc[0].copy()
49
+ new_group = pd.concat([header_row.to_frame().T, group])
50
+ new_group.iloc[0, new_group.columns.get_loc('行形式')] = 'ヘッダ'
51
+ new_group.iloc[1:, new_group.columns.get_loc('行形式')] = '明細'
52
+ new_group.iloc[0, new_group.columns.get_loc('品名')] = ''
53
+ new_group.iloc[0, new_group.columns.get_loc('数量')] = ''
54
+ new_group.iloc[0, new_group.columns.get_loc('単価')] = ''
55
+ new_group.iloc[0, new_group.columns.get_loc('単位')] = ''
56
+ new_group.iloc[0, new_group.columns.get_loc('明細ユーザ定義項目1')] = ''
57
+ clear_cols = ['得意先コード', '請求日', '請求番号', 'ユーザ定義項目1', 'タグ', '得意先名', '備考']
58
+ for col in clear_cols:
59
+ if col in new_group.columns:
60
+ new_group.iloc[1:, new_group.columns.get_loc(col)] = ''
61
+ dfs.append(new_group)
62
+
63
+ df_modified = pd.concat(dfs)
64
+
65
+ # Save the modified dataframe to the output file
66
+ output_file_path = "processed.csv"
67
+ df_modified.to_csv(output_file_path, index=False)
68
+
69
+ return output_file_path
70
+
71
+ # Save the modified dataframe to the output file
72
+ output_file_path = "processed.csv"
73
+ df_modified.to_csv(output_file_path, index=False, encoding="MS932")
74
+
75
+ return output_file_path
76
+
77
+ # Webアプリを作成
78
+ app = gr.Interface(fn=process_csv, inputs="file", outputs="file")
79
+ # Webアプリを起動
80
+ app.launch()