documents

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

md2bitwarden.py (1917B)


      1 import csv
      2 import os
      3 import re
      4 import sys
      5 
      6 
      7 def convert_md_file_to_bitwarden_csv(input_path):
      8     if not os.path.exists(input_path):
      9         print(f"Error: {input_path} not found.")
     10         return
     11 
     12     with open(input_path, "r", encoding="utf-8") as f:
     13         lines = f.readlines()
     14 
     15     data = []
     16     for line in lines:
     17         if re.match(r"^\s*\|?[:\-\s|]+\|?\s*$", line):
     18             continue
     19         columns = [col.strip() for col in line.split("|") if col.strip()]
     20         if columns:
     21             data.append(columns)
     22 
     23     if len(data) < 2:
     24         print("Error: No valid data found in table.")
     25         return
     26 
     27     headers = data[0]
     28     rows = data[1:]
     29     output_file = "bitwarden_import.csv"
     30     bw_headers = [
     31         "folder",
     32         "favorite",
     33         "type",
     34         "name",
     35         "notes",
     36         "fields",
     37         "login_uri",
     38         "login_username",
     39         "login_password",
     40         "login_totp",
     41     ]
     42 
     43     with open(output_file, "w", newline="", encoding="utf-8") as f:
     44         writer = csv.DictWriter(f, fieldnames=bw_headers)
     45         writer.writeheader()
     46 
     47         for row in rows:
     48             item = dict(zip(headers, row))
     49             writer.writerow(
     50                 {
     51                     "folder": "Main",
     52                     "favorite": 0,
     53                     "type": "login",
     54                     "name": item.get("サービス名", ""),
     55                     "login_username": item.get("ユーザー名", ""),
     56                     "login_password": item.get("パスワード", ""),
     57                 }
     58             )
     59 
     60     print(f"Success: {output_file} has been created.")
     61 
     62 
     63 if __name__ == "__main__":
     64     # 実行時にファイル名を指定するか、デフォルトの passwords.md を読み込む
     65     target_file = sys.argv[1] if len(sys.argv) > 1 else "password01.md"
     66     convert_md_file_to_bitwarden_csv(target_file)