Skip to content

Instantly share code, notes, and snippets.

@louicoder
Last active January 30, 2020 07:39
Show Gist options
  • Save louicoder/ad9545e6d1ca2e561798963372cd5bd9 to your computer and use it in GitHub Desktop.
Save louicoder/ad9545e6d1ca2e561798963372cd5bd9 to your computer and use it in GitHub Desktop.
Python script to read from Word document and populate data into an excel file
altgraph==0.17
astroid==2.3.3
docx==0.2.4
isort==4.3.21
lazy-object-proxy==1.4.3
lxml==4.4.2
mccabe==0.6.1
MouseInfo==0.1.2
Pillow==7.0.0
PyAutoGUI==0.9.48
PyGetWindow==0.0.8
PyInstaller==3.6
pylint==2.4.4
PyMsgBox==1.0.7
pyperclip==1.7.0
PyRect==0.1.4
PyScreeze==0.1.26
python-docx==0.8.10
python3-xlib==0.15
PyTweening==1.0.3
six==1.14.0
typed-ast==1.4.1
wrapt==1.11.2
XlsxWriter==1.2.7
import xlsxwriter
import glob
from docx import Document
from pyautogui import alert
workbook = xlsxwriter.Workbook('LONG LISTING.xlsx')
worksheet = workbook.add_worksheet()
# SET COLUMN HEADERS
# --- table 1
worksheet.write('A1', 'Surname') #surname
worksheet.write('B1', 'Other Names') #othername
worksheet.write('C1', 'Maiden Name') #maiden
worksheet.write('D1', 'Date of Birth') #dob
worksheet.write('E1', 'Present Nationality') #nationality
worksheet.write('F1', 'Gender') #gender
worksheet.write('G1', 'Permanent Address') #address
worksheet.write('H1', 'Telephone') #telephone
worksheet.write('I1', 'Email Address') #email
# --- Table 2
worksheet.write('J1', 'Post Graduate') # postgrad
worksheet.write('K1', 'Graduate Degree') # graduatedegree
worksheet.write('L1', 'Diploma') # Diploma
worksheet.write('M1', 'Professional Certificate') # profcert
# --- Table 3
worksheet.write('N1', 'Present Post') # Presentpost
worksheet.write('O1', 'Name Of Employer') # Nameemployer
worksheet.write('P1', 'From') # Fromm
worksheet.write('Q1', 'To') # to
worksheet.write('R1', 'Previous Post') # previouspost
worksheet.write('S1', 'Name Of Employer') # Nameemployer2
worksheet.write('T1', 'Address Of Employer') # Address
# --- Table 4
worksheet.write('U1', 'Referee 1 Name') # postgrad
worksheet.write('V1', 'Referee 1 Adress') # graduatedegree
worksheet.write('W1', 'Referee 1 Occupation') # Diploma
worksheet.write('X1', 'Referee 1 Phone Number') # profcert
worksheet.write('Y1', 'Referee 2 Name') # Presentpost
worksheet.write('Z1', 'Referee 2 Adress') # Nameemployer
worksheet.write('AA1', 'Referee 2 Occupation') # Fromm
worksheet.write('AB1', 'Referee 2 Phone Number') # to
worksheet.write('AC1', 'Referee 3 Name') # previouspost
worksheet.write('AD1', 'Referee 3 Adress') # Nameemployer2
worksheet.write('AE1', 'Referee 3 Occupation') # Address
# --- FINISH SETTING COLUMN HEADERS.
print('Satrted operation')
x = 2
# ITERATE OVER ALL DOCX AVAILABLE IN DOCS DIRECTORY
for enumerator, file in enumerate(glob.glob('./docs/*.docx')):
iterator = x + enumerator
document = Document(file)
table1 = document.tables[0]
table2 = document.tables[1]
table3 = document.tables[2]
table4 = document.tables[3]
# FIRST TABLE
surname = table1.cell(0,1).text # surname
other = table1.cell(1,1).text # other name
maiden = table1.cell(2,1).text # maiden
dob = table1.cell(3,1).text # date of birth
nationality = table1.cell(4,1).text # natonality
gender = table1.cell(5,1).text # gender
address = table1.cell(6,1).text # address
telephone = table1.cell(7,1).text # telephone
email = table1.cell(8,1).text # email
# SECOND TABLE
postgrad = table2.cell(0,1).text # postgrad
graduatedegree = table2.cell(1,1).text # graduatedegree
Diploma = table2.cell(2,1).text # Diploma
profcert = table2.cell(3,1).text # profcert
# THIRD TABLE
Presentpost = table3.cell(0,1).text # Presentpost
Nameemployer = table3.cell(1,1).text # Nameemployer
Fromm = table3.cell(2,1).text #Fromm
to = table3.cell(3,1).text #to
previouspost = table3.cell(4,1).text #previouspost
Nameemployer2 = table3.cell(5,1).text # Nameemployer2
Address = table3.cell(6, 1).text #Address
# FOURTH TABLE
ref1Name = table4.cell(0,1).text # referee 1 Name
ref1Adress = table4.cell(1,1).text # referee 1 Address
ref1Occupation = table4.cell(2,1).text # referee 1 Occupation
ref1Phone = table4.cell(3,1).text # referee 1 Phone number
ref2Name = table4.cell(4,1).text # referee 2 Name
ref2Adress = table4.cell(5,1).text # referee 2 Address
ref2Occupation = table4.cell(6, 1).text # referee 2 Occupation
ref2Phone = table4.cell(7, 1).text # referee 2 Phone number
ref3Name = table4.cell(8,1).text # referee 2 Name
ref3Adress = table4.cell(9,1).text # referee 2 Address
ref3Occupation = table4.cell(10, 1).text # referee 2 Occupation
# write to worksheet from table one information
# worksheet.write(xx, surname) #surname
worksheet.write('A'+str(iterator), surname) #surname
worksheet.write('B'+str(iterator), other) #othername
worksheet.write('C'+str(iterator), maiden) #maiden
worksheet.write('D'+str(iterator), dob) #dob
worksheet.write('E'+str(iterator), nationality) #nationality
worksheet.write('F'+str(iterator), gender) #gender
worksheet.write('G'+str(iterator), address) #address
worksheet.write('H'+str(iterator), telephone) #telephone
worksheet.write('I'+str(iterator), email) #email
worksheet.write('J'+str(iterator), postgrad) # postgrad
worksheet.write('K'+str(iterator), graduatedegree) # graduatedegree
worksheet.write('L'+str(iterator), Diploma) # Diploma
worksheet.write('M'+str(iterator), profcert) # profcert
worksheet.write('N'+str(iterator), Presentpost) # Presentpost
worksheet.write('O'+str(iterator), Nameemployer) # Nameemployer
worksheet.write('P'+str(iterator), Fromm) # Fromm
worksheet.write('Q'+str(iterator), to) # to
worksheet.write('R'+str(iterator), previouspost) # previouspost
worksheet.write('S'+str(iterator), Nameemployer2) # Nameemployer2
worksheet.write('T'+str(iterator), Address) # Address
worksheet.write('U'+str(iterator), ref1Name) # postgrad
worksheet.write('V'+str(iterator), ref1Adress) # graduatedegree
worksheet.write('W'+str(iterator), ref1Occupation) # Diploma
worksheet.write('X'+str(iterator), ref1Phone) # profcert
worksheet.write('Y'+str(iterator), ref2Name) # Presentpost
worksheet.write('Z'+str(iterator), ref2Adress) # Nameemployer
worksheet.write('AA'+str(iterator), ref2Occupation) # Fromm
worksheet.write('AB'+str(iterator), ref2Phone) # to
worksheet.write('AC'+str(iterator), ref3Name) # previouspost
worksheet.write('AD'+str(iterator), ref3Adress) # Nameemployer2
worksheet.write('AE'+str(iterator), ref3Occupation) # Address
workbook.close()
# print('ended operation')
alert(text='Successfully completed the operation', title='Finished', button='OK')
@louicoder
Copy link
Author

@Stuartkal check this out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment