0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel から MS Access にデータ移行

Last updated at Posted at 2025-03-31

Excel から MS Access にデータを移行するための Python プログラムを作成しました。
まず、 Excel ファイルを csv (テキストファイル)として保存します。
下記のプログラムを実行して、csv ファイルを xml ファイルに変換します。
データは、Excel の1行を Access の1レコードとします。
Access では、左側のテーブル名において、マウス右クリックでインポートを選択して、XMLファイルを取り込みます。
テーブルのXMLファイルの形式は、一度データをエクスポートすれば確認できます。
テーブル構成(スキーマ)はインポートせず、データのみをインポートします。

Python 準備
mkdir 20250107_work
cd 20250107_work
python3 -m venv .pandas
source .pandas/bin/activate
(.pandas) webmaster@ubuntu:~/20250107_work$ pip install pandas

実行
cd 20250107_work
source .pandas/bin/activate
(.pandas) webmaster@ubuntu:~/20250107_work$ python excel_access_convert.py > CustInfo01.xml

excel_access_convert.py
import pandas as pd
from datetime import datetime

# T_CustomerInformation

# CSVファイルを読み込む
file_path = './ExcelCustomerInfo01.csv'
data = pd.read_csv(file_path)

row_count = 1 # 行番号
start_row = 987  # 開始行
end_row = 1019  # 終了行

# ヘッダー
print(f"<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
print(f"<dataroot xmlns:od=\"urn:schemas-microsoft-com:officedata\" generated=\"2025-03-21T13:38:14\">")


# 各行のデータを指定されたXML形式で出力する
for index, row in data.iterrows():
    row_count = row_count + 1
    if row_count < start_row:
        continue
    if row_count > end_row:
        continue

    print(f"<T_CustomerInformation>")

    # print(f"<Access列名>{row['Excel列名']}</Access列名>")

    #print(f"<CustomerInformationID>{row['CustomerInformationID']}</CustomerInformationID>") # AutoID
    print(f"<DataID>1</DataID>")
    print(f"<AttachFlag>0</AttachFlag>")

    print(f"<CustomerID>{row['CustomerID']}</CustomerID>")
    print(f"<TypeID>{row['TypeID']}</TypeID>")

    # 日付の形式を変更
    date_obj = datetime.strptime(row['Date'], '%Y/%m/%d')
    receive_date = date_obj.strftime('%Y-%m-%d')
    print(f"<ReceiveDate>{receive_date}T00:00:00</ReceiveDate>")

    # 数値のカンマを除去
    item_units = row['ItemUnits'].replace(',', '')
    pre_charge = row['PreCharge'].replace(',', '')
    bill_amount = row['BillAmount'].replace(',', '')
    print(f"<ItemUnits>{item_units}</ItemUnits>")
    print(f"<PreCharge>{pre_charge}</PreCharge>")
    print(f"<BillAmount>{bill_amount}</BillAmount>")

    print(f"</T_CustomerInformation>")
    print()  # 読みやすさのためにエントリ間に空行を入れる

# フッター
print(f"</dataroot>")

 

Excel で「名前を付けて保存」から csv 形式で保存します。編集して、データが入っていない余分な行や、不要な行を削除します(開始行と終了行しか指定できないので)。

0331_01.png

 

MS Access で左側のテーブル名でマウス右クリックすると表示されるメニューです。XML ファイル形式以外もありますが、自由に加工できるようにするため、XML ファイル形式を使用します。

 

 

 

 

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?