Create CSV file to for import new users to the MS365. You will need at least First and last name for users. The script will do everything else:

  • Creates Username - first.last@domain.tld
  • Generate passwords with lenght of 12 (you can update script to have different lenght)
  • Creates display name
  • generate emails.csv file suitable for import to MS365

Script vill look for export.csv as input in the same folder as the .py script is.

"""
Description KR: Microsoft 365 μ‚¬μš©μžλ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•΄ 이름과 성이 ν¬ν•¨λœ CSV νŒŒμΌμ„ CSV ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€
Description EN: Convert a CSV file containing first and last names into a CSV format for importing Microsoft 365 users
"""
import pandas as pd
from unidecode import unidecode
import random
import string

domain_name = "@domain.name" # Update this variable with your own domain / μžμ‹ μ˜ λ„λ©”μΈμœΌλ‘œ 이 λ³€μˆ˜λ₯Ό μ—…λ°μ΄νŠΈν•˜μ„Έμš”.

# 각 μ‚¬μš©μžμ— λŒ€ν•΄ 길이 12둜 μ•”ν˜Έ 생성
# generate password with lenght 12 for each user
def generate_password(lenght=12):
    characters = string.ascii_letters.replace('l', '').replace('O', '') + string.digits
    password = ''.join(random.choice(characters) for i in range(lenght))
    return password

df = pd.read_csv('export.csv', delimiter=';')

df = df.iloc[:, :2]
df.columns = ['Last name', 'First name']

# 곡백을 μ œκ±°ν•˜κ³  μ•…μ„ΌνŠΈκ°€ μžˆλŠ” 문자λ₯Ό μ•”μ†‘ν•©λ‹ˆλ‹€
# remove spaces and transliterate accented characters
first_name = df['First name'].apply(lambda x: unidecode(x).replace(' ', '').lower().strip())
last_name = df['Last name'].apply(lambda x: unidecode(x).lower().strip())

df['Last name'] = df['Last name'].apply(lambda x: x.capitalize().strip())
df['Username'] = first_name + '.' + last_name + domain_name
# df['Password'] = df.apply(lambda x: generate_password(), axis=1)
df['Display name'] = df['First name'] + ' ' + df['Last name']
df['Job title'] = ''
df['Department'] = ''
df['Office number'] = ''
df['Office phone'] = ''
df['Mobile phone'] = ''
df['Fax'] = ''
df['Alternate email address'] = ''
df['Address'] = ''
df['City'] = ''
df['State or province'] = ''
df['ZIP or postal code'] = ''
df['Country or region'] = ''

# print(df.head())

# μƒˆ 파일둜 내보내기 전에 μ—΄ μž¬μ •λ ¬
# reorder columns before exporting to new file
ordered_columns = ['Username',
                    'First name',
                    'Last name',
                    'Display name',
                    'Job title',
                    'Department',
                    'Office number',
                    'Office phone',
                    'Mobile phone',
                    'Fax',
                    'Alternate email address',
                    'Address',
                    'City',
                    'State or province',
                    'ZIP or postal code',
                    'Country or region']

df.to_csv('emails.csv', columns=ordered_columns, index=False)

Upload the emails.csv file to the MS365 user import to create users.