O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Automate the Boring Stuff with Python, 2nd Edition

Book Description

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.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. About the Author
  6. About the Technical Reviewer
  7. BRIEF CONTENTS
  8. CONTENTS IN DETAIL
  9. ACKNOWLEDGMENTS
  10. INTRODUCTION
    1. Whom Is This Book For?
    2. Conventions
    3. What Is Programming?
    4. About This Book
    5. Downloading and Installing Python
    6. Downloading and Installing Mu
    7. Starting Mu
    8. Starting IDLE
    9. The Interactive Shell
    10. Installing Third-Party Modules
    11. How to Find Help
    12. Asking Smart Programming Questions
    13. Summary
  11. PART I: PYTHON PROGRAMMING BASICS
  12. 1 PYTHON BASICS
    1. Entering Expressions into the Interactive Shell
    2. The Integer, Floating-Point, and String Data Types
    3. String Concatenation and Replication
    4. Storing Values in Variables
    5. Your First Program
    6. Dissecting Your Program
    7. Summary
    8. Practice Questions
  13. 2 FLOW CONTROL
    1. Boolean Values
    2. Comparison Operators
    3. Boolean Operators
    4. Mixing Boolean and Comparison Operators
    5. Elements of Flow Control
    6. Program Execution
    7. Flow Control Statements
    8. Importing Modules
    9. Ending a Program Early with the sys.exit() Function
    10. A Short Program: Guess the Number
    11. A Short Program: Rock, Paper, Scissors
    12. Summary
    13. Practice Questions
  14. 3 FUNCTIONS
    1. def Statements with Parameters
    2. Return Values and return Statements
    3. The None Value
    4. Keyword Arguments and the print() Function
    5. The Call Stack
    6. Local and Global Scope
    7. The global Statement
    8. Exception Handling
    9. A Short Program: Zigzag
    10. Summary
    11. Practice Questions
    12. Practice Projects
  15. 4 LISTS
    1. The List Data Type
    2. Working with Lists
    3. Augmented Assignment Operators
    4. Methods
    5. Example Program: Magic 8 Ball with a List
    6. Sequence Data Types
    7. References
    8. A Short Program: Conway’s Game of Life
    9. Summary
    10. Practice Questions
    11. Practice Projects
  16. 5 DICTIONARIES AND STRUCTURING DATA
    1. The Dictionary Data Type
    2. Pretty Printing
    3. Using Data Structures to Model Real-World Things
    4. Summary
    5. Practice Questions
    6. Practice Projects
  17. 6 MANIPULATING STRINGS
    1. Working with Strings
    2. Putting Strings Inside Other Strings
    3. Useful String Methods
    4. Numeric Values of Characters with the ord() and chr() Functions
    5. Copying and Pasting Strings with the pyperclip Module
    6. Project: Multi-Clipboard Automatic Messages
    7. Project: Adding Bullets to Wiki Markup
    8. A Short Progam: Pig Latin
    9. Summary
    10. Practice Questions
    11. Practice Projects
  18. PART II: AUTOMATING TASKS
  19. 7 PATTERN MATCHING WITH REGULAR EXPRESSIONS
    1. Finding Patterns of Text Without Regular Expressions
    2. Finding Patterns of Text with Regular Expressions
    3. More Pattern Matching with Regular Expressions
    4. Greedy and Non-greedy Matching
    5. The findall() Method
    6. Character Classes
    7. Making Your Own Character Classes
    8. The Caret and Dollar Sign Characters
    9. The Wildcard Character
    10. Review of Regex Symbols
    11. Case-Insensitive Matching
    12. Substituting Strings with the sub() Method
    13. Managing Complex Regexes
    14. Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE
    15. Project: Phone Number and Email Address Extractor
    16. Summary
    17. Practice Questions
    18. Practice Projects
  20. 8 INPUT VALIDATION
    1. The PyInputPlus Module
    2. Project: How to Keep an Idiot Busy for Hours
    3. Project: Multiplication Quiz
    4. Summary
    5. Practice Questions
    6. Practice Projects
  21. 9 READING AND WRITING FILES
    1. Files and File Paths
    2. The File Reading/Writing Process
    3. Saving Variables with the shelve Module
    4. Saving Variables with the pprint.pformat() Function
    5. Project: Generating Random Quiz Files
    6. Project: Updatable Multi-Clipboard
    7. Summary
    8. Practice Questions
    9. Practice Projects
  22. 10 ORGANIZING FILES
    1. The shutil Module
    2. Walking a Directory Tree
    3. Compressing Files with the zipfile Module
    4. Project: Renaming Files with American-Style Dates to European-Style Dates
    5. Project: Backing Up a Folder into a ZIP File
    6. Summary
    7. Practice Questions
    8. Practice Projects
  23. 11 DEBUGGING
    1. Raising Exceptions
    2. Getting the Traceback as a String
    3. Assertions
    4. Logging
    5. Mu’s Debugger
    6. Summary
    7. Practice Questions
    8. Practice Project
  24. 12 WEB SCRAPING
    1. Project: mapIt.py with the webbrowser Module
    2. Downloading Files from the Web with the requests Module
    3. Saving Downloaded Files to the Hard Drive
    4. HTML
    5. Parsing HTML with the bs4 Module
    6. Project: Opening All Search Results
    7. Project: Downloading All XKCD Comics
    8. Controlling the Browser with the selenium Module
    9. Summary
    10. Practice Questions
    11. Practice Projects
  25. 13 WORKING WITH EXCEL SPREADSHEETS
    1. Excel Documents
    2. Installing the openpyxl Module
    3. Reading Excel Documents
    4. Project: Reading Data from a Spreadsheet
    5. Writing Excel Documents
    6. Project: Updating a Spreadsheet
    7. Setting the Font Style of Cells
    8. Font Objects
    9. Formulas
    10. Adjusting Rows and Columns
    11. Charts
    12. Summary
    13. Practice Questions
    14. Practice Projects
  26. 14 WORKING WITH GOOGLE SHEETS
    1. Installing and Setting Up EZSheets
    2. Spreadsheet Objects
    3. Sheet Objects
    4. Working with Google Sheets Quotas
    5. Summary
    6. Practice Questions
    7. Practice Projects
  27. 15 WORKING WITH PDF AND WORD DOCUMENTS
    1. PDF Documents
    2. Project: Combining Select Pages from Many PDFs
    3. Word Documents
    4. Creating PDFs from Word Documents
    5. Summary
    6. Practice Questions
    7. Practice Projects
  28. 16 WORKING WITH CSV FILES AND JSON DATA
    1. The csv Module
    2. Project: Removing the Header from CSV Files
    3. JSON and APIs
    4. The json Module
    5. Project: Fetching Current Weather Data
    6. Summary
    7. Practice Questions
    8. Practice Project
  29. 17 KEEPING TIME, SCHEDULING TASKS, AND LAUNCHING PROGRAMS
    1. The time Module
    2. Rounding Numbers
    3. Project: Super Stopwatch
    4. The datetime Module
    5. Review of Python’s Time Functions
    6. Multithreading
    7. Project: Multithreaded XKCD Downloader
    8. Launching Other Programs from Python
    9. Project: Simple Countdown Program
    10. Summary
    11. Practice Questions
    12. Practice Projects
  30. 18 SENDING EMAIL AND TEXT MESSAGES
    1. Sending and Receiving Email with the Gmail API
    2. SMTP
    3. Sending Email
    4. IMAP
    5. Retrieving and Deleting Emails with IMAP
    6. Project: Sending Member Dues Reminder Emails
    7. Sending Text Messages with SMS Email Gateways
    8. Sending Text Messages with Twilio
    9. Project: “Just Text Me” Module
    10. Summary
    11. Practice Questions
    12. Practice Projects
  31. 19 MANIPULATING IMAGES
    1. Computer Image Fundamentals
    2. Manipulating Images with Pillow
    3. Project: Adding a Logo
    4. Drawing on Images
    5. Summary
    6. Practice Questions
    7. Practice Projects
  32. 20 CONTROLLING THE KEYBOARD AND MOUSE WITH GUI AUTOMATION
    1. Installing the pyautogui Module
    2. Setting Up Accessibility Apps on macOS
    3. Staying on Track
    4. Controlling Mouse Movement
    5. Controlling Mouse Interaction
    6. Planning Your Mouse Movements
    7. Working with the Screen
    8. Image Recognition
    9. Getting Window Information
    10. Controlling the Keyboard
    11. Setting Up Your GUI Automation Scripts
    12. Review of the PyAutoGUI Functions
    13. Project: Automatic Form Filler
    14. Displaying Message Boxes
    15. Summary
    16. Practice Questions
    17. Practice Projects
  33. A INSTALLING THIRD-PARTY MODULES
    1. The pip Tool
    2. Installing Third-Party Modules
    3. Installing Modules for the Mu Editor
  34. B RUNNING PROGRAMS
    1. Running Programs from the Terminal Window
    2. Running Python Programs on Windows
    3. Running Python Programs on macOS
    4. Running Python Programs on Ubuntu Linux
    5. Running Python Programs with Assertions Disabled
  35. C ANSWERS TO THE PRACTICE QUESTIONS
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18
    19. Chapter 19
    20. Chapter 20
  36. INDEX