File size: 3,103 Bytes
b670651
 
 
 
 
 
 
8915859
b670651
 
 
 
 
 
 
dc6b00a
5792574
b670651
 
 
 
 
 
 
 
 
 
dc6b00a
 
ee8c281
b670651
 
 
 
6a92c8b
 
ee8c281
6a92c8b
 
b670651
 
 
 
 
 
 
 
 
 
 
 
 
ee8c281
dc6b00a
b670651
 
 
 
 
 
 
 
6dd762f
b61f5cf
b670651
 
 
dc6b00a
 
56c0e51
dc6b00a
 
 
b670651
 
 
b27ce04
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
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 = {
        '伝票日付': '請求日',
        '伝票番号': '請求番号',
        '商品名': '品名',
        '売上数量_請求用数量': '数量',
        '売上単価': '単価',
        '得意先名': 'タグ',
        '行摘要': '得意先名',
        'ユーザー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['請求日']


    # 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 = ['得意先コード', '請求日', '請求番号', 'タグ', '得意先名', 'メモ']
        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(debug=True)