Processing Images, Excel, and Word Documents with Python (Pillow, openpyxl, python-docx)
This tutorial demonstrates how to use Python's Pillow library for image manipulation, openpyxl for Excel spreadsheet handling, and python-docx for creating and editing Word documents, providing code examples for common operations such as cropping, resizing, filtering, and data insertion.
Python can be used to manipulate images and office documents even though the standard library lacks direct support; third‑party modules fill the gap.
Image Operations
Understanding basic concepts such as RGB color model and pixels is essential. The RGB values for common colors are listed in a table.
Install Pillow, the actively maintained fork of PIL, with:
pip install pillowThe core class is Image . Example usage:
>>> from PIL import Image
>>> image = Image.open('./res/guido.jpg')
>>> image.format, image.size, image.mode
('JPEG', (500, 750), 'RGB')
>>> image.show()Common operations include:
Crop an image: >>> rect = 80, 20, 310, 360 >>> image.crop(rect).show()
Create a thumbnail: >>> size = 128, 128 >>> image.thumbnail(size) >>> image.show()
Resize and paste another image: >>> image1 = Image.open('./res/luohao.png') >>> image2 = Image.open('./res/guido.jpg') >>> rect = 80, 20, 310, 360 >>> guido_head = image2.crop(rect) >>> width, height = guido_head.size >>> image1.paste(guido_head.resize((int(width/1.5), int(height/1.5))), (172, 40)) >>> image1.show()
Rotate and flip: >>> image = Image.open('./res/guido.png') >>> image.rotate(180).show() >>> image.transpose(Image.FLIP_LEFT_RIGHT).show()
Pixel‑level manipulation: >>> for x in range(80, 310): for y in range(20, 360): image.putpixel((x, y), (128, 128, 128)) >>> image.show()
Apply filters such as contour: >>> from PIL import Image, ImageFilter >>> image = Image.open('./res/guido.jpg') >>> image.filter(ImageFilter.CONTOUR).show()
Processing Excel Spreadsheets
The openpyxl module enables reading and writing of modern Excel files (xlsx). Example:
import datetime
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = 42
ws.append([1, 2, 3])
ws['A2'] = datetime.datetime.now()
wb.save("sample.xlsx")Processing Word Documents
The python-docx library allows creation and modification of .docx files. Example workflow:
from docx import Document
from docx.shared import Inches
document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')
document.add_paragraph('first item in unordered list', style='List Bullet')
document.add_paragraph('first item in ordered list', style='List Number')
document.add_picture('monty-truth.png', width=Inches(1.25))
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = id
row_cells[2].text = desc
document.add_page_break()
document.save('demo.docx')The article concludes with a disclaimer that the content is collected from the internet and the original author holds the copyright.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.