The second edition of this best-selling Python book (100,000+ copies sold in print alone) uses Python 3 to teach even the technically uninclined how to write programs that do in minutes what would take hours to do by hand. There is no prior programming experience required and the book is loved by liberal arts majors and geeks alike.
If you've ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?
In this fully revised second edition of the best-selling classic Automate the Boring Stuff with Python, you'll learn how to use Python to write programs that do in minutes what would take you hours to do by hand--no prior programming experience required. You'll learn the basics Python and explore Python's rich library of modules for performing specific tasks, like scraping data off websites, reading PDF and Word documents, and automating clicking and typing tasks.
The second edition of this international fan favorite includes a brand-new chapter on input validation, as well as tutorials on automating Gmail and Google Sheets, plus tips on automatically updating CSV files. You'll learn how to create programs that effortlessly perform useful feats of automation to:
- Search for text in a file or across multiple files
- Create, update, move, and rename files and folders
- Search the Web and download online content
- Update and format data in Excel spreadsheets of any size
- Split, merge, watermark, and encrypt PDFs
- Send email responses and text notifications
- Fill out online forms
Step-by-step instructions walk you through each program, and updated practice projects at the end of each chapter challenge you to improve those programs and use your newfound skills to automate similar tasks.
Don't spend your time doing work a well-trained monkey could do. Even if you've never written a line of code, you can make your computer do the grunt work. Learn how in Automate the Boring Stuff with Python, 2nd Edition.
Author(s): Al Sweigart
Edition: 2
Cover Page
Title Page
Copyright Page
Dedication
About the Author
About the Technical Reviewer
BRIEF CONTENTS
CONTENTS IN DETAIL
ACKNOWLEDGMENTS
INTRODUCTION
Whom Is This Book For?
Conventions
What Is Programming?
About This Book
Downloading and Installing Python
Downloading and Installing Mu
Starting Mu
Starting IDLE
The Interactive Shell
Installing Third-Party Modules
How to Find Help
Asking Smart Programming Questions
Summary
PART I: PYTHON PROGRAMMING BASICS
1 PYTHON BASICS
Entering Expressions into the Interactive Shell
The Integer, Floating-Point, and String Data Types
String Concatenation and Replication
Storing Values in Variables
Your First Program
Dissecting Your Program
Summary
Practice Questions
2 FLOW CONTROL
Boolean Values
Comparison Operators
Boolean Operators
Mixing Boolean and Comparison Operators
Elements of Flow Control
Program Execution
Flow Control Statements
Importing Modules
Ending a Program Early with the sys.exit() Function
A Short Program: Guess the Number
A Short Program: Rock, Paper, Scissors
Summary
Practice Questions
3 FUNCTIONS
def Statements with Parameters
Return Values and return Statements
The None Value
Keyword Arguments and the print() Function
The Call Stack
Local and Global Scope
The global Statement
Exception Handling
A Short Program: Zigzag
Summary
Practice Questions
Practice Projects
4 LISTS
The List Data Type
Working with Lists
Augmented Assignment Operators
Methods
Example Program: Magic 8 Ball with a List
Sequence Data Types
References
A Short Program: Conway’s Game of Life
Summary
Practice Questions
Practice Projects
5 DICTIONARIES AND STRUCTURING DATA
The Dictionary Data Type
Pretty Printing
Using Data Structures to Model Real-World Things
Summary
Practice Questions
Practice Projects
6 MANIPULATING STRINGS
Working with Strings
Putting Strings Inside Other Strings
Useful String Methods
Numeric Values of Characters with the ord() and chr() Functions
Copying and Pasting Strings with the pyperclip Module
Project: Multi-Clipboard Automatic Messages
Project: Adding Bullets to Wiki Markup
A Short Progam: Pig Latin
Summary
Practice Questions
Practice Projects
PART II: AUTOMATING TASKS
7 PATTERN MATCHING WITH REGULAR EXPRESSIONS
Finding Patterns of Text Without Regular Expressions
Finding Patterns of Text with Regular Expressions
More Pattern Matching with Regular Expressions
Greedy and Non-greedy Matching
The findall() Method
Character Classes
Making Your Own Character Classes
The Caret and Dollar Sign Characters
The Wildcard Character
Review of Regex Symbols
Case-Insensitive Matching
Substituting Strings with the sub() Method
Managing Complex Regexes
Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE
Project: Phone Number and Email Address Extractor
Summary
Practice Questions
Practice Projects
8 INPUT VALIDATION
The PyInputPlus Module
Project: How to Keep an Idiot Busy for Hours
Project: Multiplication Quiz
Summary
Practice Questions
Practice Projects
9 READING AND WRITING FILES
Files and File Paths
The File Reading/Writing Process
Saving Variables with the shelve Module
Saving Variables with the pprint.pformat() Function
Project: Generating Random Quiz Files
Project: Updatable Multi-Clipboard
Summary
Practice Questions
Practice Projects
10 ORGANIZING FILES
The shutil Module
Walking a Directory Tree
Compressing Files with the zipfile Module
Project: Renaming Files with American-Style Dates to European-Style Dates
Project: Backing Up a Folder into a ZIP File
Summary
Practice Questions
Practice Projects
11 DEBUGGING
Raising Exceptions
Getting the Traceback as a String
Assertions
Logging
Mu’s Debugger
Summary
Practice Questions
Practice Project
12 WEB SCRAPING
Project: mapIt.py with the webbrowser Module
Downloading Files from the Web with the requests Module
Saving Downloaded Files to the Hard Drive
HTML
Parsing HTML with the bs4 Module
Project: Opening All Search Results
Project: Downloading All XKCD Comics
Controlling the Browser with the selenium Module
Summary
Practice Questions
Practice Projects
13 WORKING WITH EXCEL SPREADSHEETS
Excel Documents
Installing the openpyxl Module
Reading Excel Documents
Project: Reading Data from a Spreadsheet
Writing Excel Documents
Project: Updating a Spreadsheet
Setting the Font Style of Cells
Font Objects
Formulas
Adjusting Rows and Columns
Charts
Summary
Practice Questions
Practice Projects
14 WORKING WITH GOOGLE SHEETS
Installing and Setting Up EZSheets
Spreadsheet Objects
Sheet Objects
Working with Google Sheets Quotas
Summary
Practice Questions
Practice Projects
15 WORKING WITH PDF AND WORD DOCUMENTS
PDF Documents
Project: Combining Select Pages from Many PDFs
Word Documents
Creating PDFs from Word Documents
Summary
Practice Questions
Practice Projects
16 WORKING WITH CSV FILES AND JSON DATA
The csv Module
Project: Removing the Header from CSV Files
JSON and APIs
The json Module
Project: Fetching Current Weather Data
Summary
Practice Questions
Practice Project
17 KEEPING TIME, SCHEDULING TASKS, AND LAUNCHING PROGRAMS
The time Module
Rounding Numbers
Project: Super Stopwatch
The datetime Module
Review of Python’s Time Functions
Multithreading
Project: Multithreaded XKCD Downloader
Launching Other Programs from Python
Project: Simple Countdown Program
Summary
Practice Questions
Practice Projects
18 SENDING EMAIL AND TEXT MESSAGES
Sending and Receiving Email with the Gmail API
SMTP
Sending Email
IMAP
Retrieving and Deleting Emails with IMAP
Project: Sending Member Dues Reminder Emails
Sending Text Messages with SMS Email Gateways
Sending Text Messages with Twilio
Project: “Just Text Me” Module
Summary
Practice Questions
Practice Projects
19 MANIPULATING IMAGES
Computer Image Fundamentals
Manipulating Images with Pillow
Project: Adding a Logo
Drawing on Images
Summary
Practice Questions
Practice Projects
20 CONTROLLING THE KEYBOARD AND MOUSE WITH GUI AUTOMATION
Installing the pyautogui Module
Setting Up Accessibility Apps on macOS
Staying on Track
Controlling Mouse Movement
Controlling Mouse Interaction
Planning Your Mouse Movements
Working with the Screen
Image Recognition
Getting Window Information
Controlling the Keyboard
Setting Up Your GUI Automation Scripts
Review of the PyAutoGUI Functions
Project: Automatic Form Filler
Displaying Message Boxes
Summary
Practice Questions
Practice Projects
A INSTALLING THIRD-PARTY MODULES
The pip Tool
Installing Third-Party Modules
Installing Modules for the Mu Editor
B RUNNING PROGRAMS
Running Programs from the Terminal Window
Running Python Programs on Windows
Running Python Programs on macOS
Running Python Programs on Ubuntu Linux
Running Python Programs with Assertions Disabled
C ANSWERS TO THE PRACTICE QUESTIONS
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
INDEX