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.