Skip to main content
Practice

Introduction to Python Libraries for Office Automation

Different libraries are used for automating tasks in various office programs like Excel, Word, and PowerPoint.

In this lesson, we will introduce the automation libraries for each office program and explore how to use them through simple example codes.


Excel Automation Libraries: pandas and openpyxl

When working with Excel files, you might often need to perform repetitive tasks such as entering large amounts of data or merging multiple Excel files.

In such cases, libraries like pandas and openpyxl can help you easily automate Excel tasks.

Note: To run the practice codes on your computer, install the pandas and openpyxl libraries using the pip install pandas openpyxl command.


Manipulating and Analyzing Data with pandas

pandas is a Python library specialized for data analysis, allowing you to quickly and easily perform tasks such as aggregating, sorting, and classifying large datasets.

To this end, pandas provides a data structure called DataFrame, which is composed of rows and columns like a table.

Creating a DataFrame with pandas
import pandas as pd

# Creating a DataFrame
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 22, 30, 35],
'city': ['New York', 'Los Angeles', 'Chicago', 'Houston']
})

# Printing the DataFrame
print(df)
Output
      name  age        city
0 Alice 25 New York
1 Bob 22 Los Angeles
2 Charlie 30 Chicago
3 David 35 Houston

With pandas, you can read Excel files into DataFrames, perform various operations, and systematically handle complex and vast datasets.


Handling Excel Files with openpyxl

openpyxl is a Python library used for creating, modifying, and saving Excel files.

Using this library, you can perform various Excel tasks like merging files separated by tabs or modifying the values in specific cells.

Modifying an Excel File with openpyxl
from openpyxl import load_workbook

# Opening an Excel file
workbook = load_workbook('input_file.xlsx')

# Selecting a specific sheet
sheet = workbook['Sheet1']

# Modifying cell value
sheet['A1'] = 'Hello, Excel!'

# Saving changes
workbook.save('output_file.xlsx')

PowerPoint Automation Library: python-pptx

python-pptx is a Python library for creating and editing PowerPoint slides.

Utilizing this library, you can programmatically add or modify text, images, charts, and other elements in your slides.

Creating a PowerPoint Slide with python-pptx
from pptx import Presentation

# Creating a new presentation
prs = Presentation()

# Adding a title slide
slide = prs.slides.add_slide(prs.slide_layouts[0])

# Setting title and subtitle
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Welcome to Python-PPTX"
subtitle.text = "Automating PowerPoint is fun!"

# Saving the file
prs.save('output_file.pptx')

Word Automation Library: python-docx

To automate document creation and editing, you can use the python-docx library.

This library helps you programmatically automate tasks such as adding text and creating tables in Word documents using Python.

Creating a Word Document with python-docx
from docx import Document

# Creating a new document
doc = Document()

# Adding a title
doc.add_heading('Automating Word with Python', 0)

# Adding a paragraph
doc.add_paragraph('This document was created using python-docx.')

# Saving the document
doc.save('output_file.docx')

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.