Day 28 - Play with MS-Word using Python Script


Sometimes we have to scan the data from MS-Word so that we would  proceed with next set of activities.

Here you go, buddy !!!!

You can use this script to read the content of MS-word + Add a new paragraph in the doc + Add a new image in the doc

Python Script:

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 16 18:43:01 2020

@author: Rakesh.Ranjan
"""
import docx
import pandas as pd

doc = docx.Document("C:\\Users\\Rakesh.Ranjan\\Desktop\\LND\\MS-DOC.docx")

#reading 1st paragraph
single_para = doc.paragraphs[0]
print(single_para.text)

line_number = 0

#Reading all Paragraphs
all_paras = doc.paragraphs
print(len(all_paras))

#creating an empty dataframe
column_names = ["line_number","line_text"]
doc_df = pd.DataFrame(columns = column_names)

#reading each paragraph and storing it in the dataframe
for para in all_paras:
    doc_df = doc_df.append({"line_number":line_number, "line_text":para.text},ignore_index=True)
    line_number = line_number + 1


#add paragraph in the document
doc.add_paragraph("This is last paragraph of a MS Word file.")
doc.save("C:\\Users\\Rakesh.Ranjan\\Desktop\\LND\\MS-DOC.docx")

#add image in the document
doc.add_picture("C:\\Users\\Rakesh.Ranjan\\Desktop\\LND\\Annotation 2020-04-15 081240.png", width=docx.shared.Inches(5), height=docx.shared.Inches(7))
doc.save("C:\\Users\\Rakesh.Ranjan\\Desktop\\LND\\MS-DOC.docx")

MS-DOC content:




Output:










#RR #Day28 #msword #docx #read #addparagraph #addimage #HappyLearning #WeLearnEveryday

Comments

Popular posts from this blog

Day 32 - Python Script to track Available COVID-19 Vaccine Slots for 18+ in India

DAY 1 - Steps to prepare your windows laptop for Python Programming

Day 26 - Call Power BI REST APIs to get POWER BI REPORT Details