Python Automation For Dummies (Alan Simpson) (Z-Library)

Author: Alan Simpson

技术

Streamline Your Workflow and Boost Productivity with Python Automation In today's workplaces, there's a high demand for know-how on the Python programming language, especially for writing time-saving scripts that can simplify routine work tasks. Python Automation For Dummies delivers, with simple explanations of how you can use Python to automatically wrangle data files, manage media files, create shortcuts, find and organize web data, and even analyze social media for trends. With this easy-to-follow Dummies guide, you can upskill, expand your productivity, and speed up the process of generating data-driven insights. You'll even learn to enhance your Python automations with AI, for workflows that are faster and smarter. Review the basics of Python coding and follow steps for automating all sorts of tasks Manage large file sets, organize and analyze data, and speed up research processes Automate scheduling and other time-consuming tasks—and optimize work with AI Free up time and resources by automating routine work, so you can foucs on more important issues This is a great Dummies resource for Python developers interested in applying the popular coding language to make workflows more efficient.

📄 File Format: PDF
💾 File Size: 6.4 MB
71
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
(This page has no text content)
📄 Page 3
Python® Automation by Alan Simpson
📄 Page 4
Python® Automation For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2026 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies. Media and software compilation copyright © 2026 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. The manufacturer’s authorized representative according to the EU General Product Safety Regulation is Wiley-VCH GmbH, Boschstr. 12, 69469 Weinheim, Germany, e-mail: Product_Safety@wiley.com. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. Python is a trademark or registered trademark of Python Software Foundation in the United States. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. CERTAIN AI SYSTEMS HAVE BEEN USED IN THE CREATION OF THIS WORK. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS.  THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/ OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media that is not included in the version you purchased, you may download this material at http://booksupport. wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number is available from the publisher. ISBN 978-1-394-37142-6 (pbk); ISBN 978-1-394-37144-0 (ebk); ISBN 978-1-394-37143-3 (ebk)
📄 Page 5
Table of Contents iii Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 PART 1: GETTING STARTED WITH PYTHON AUTOMATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 CHAPTER 1: Automating with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Choosing a Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Understanding Python Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Getting Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Identifying the hardware requirements . . . . . . . . . . . . . . . . . . . . . . .11 Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 CHAPTER 2: Choosing a Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Installing VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 Installing Python Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 Creating a Folder for a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Opening a project’s folder in VS Code . . . . . . . . . . . . . . . . . . . . . . . .20 Selecting your Python version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Opening the Terminal in VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Checking your Python version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 Using Virtual Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Creating a virtual environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Activating a virtual environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Installing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 Writing and Running Python Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 Writing a Python script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 Running a Python script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 CHAPTER 3: Python Basics for Automation . . . . . . . . . . . . . . . . . . . . . . . . 35 Understanding Python Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Mastering Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Working with numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Working with text (strings) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Deciding true or false with Booleans . . . . . . . . . . . . . . . . . . . . . . . . .39 Using lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 Making immutable lists with tuples . . . . . . . . . . . . . . . . . . . . . . . . . .40
📄 Page 6
iv Python Automation For Dummies Defining key–value pairs in dictionaries . . . . . . . . . . . . . . . . . . . . . . .40 Leaving things hanging with None . . . . . . . . . . . . . . . . . . . . . . . . . . .41 Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 Dealing with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Manipulating Data with Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Using arithmetic and string operators . . . . . . . . . . . . . . . . . . . . . . . .47 Using assignment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Recognizing other operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 Getting Loopy with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Looping with for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Looping for a while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Bailing out of loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Deciding with if. . .else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Compacting decisions with a ternary operator . . . . . . . . . . . . . . . . .56 Deciding with match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Defining Python Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 Defining default values for parameters . . . . . . . . . . . . . . . . . . . . . . .60 Using type hints in Python functions . . . . . . . . . . . . . . . . . . . . . . . . .60 Creating Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 PART 2: AUTOMATING COMMON COMPUTER TASKS . . . . . 67 CHAPTER 4: Automating Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . 69 Demystifying the Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Drives, directories, folders, and files . . . . . . . . . . . . . . . . . . . . . . . . . .71 Absolute versus relative paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 Backslashes in Windows paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 Playing It Safe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74 Navigating Folders and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74 Organizing Files by Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 Using mkdir for subfolders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Moving files with shutil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Making the script your own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Renaming Files in Bulk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Renaming files with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Using the bulk renaming script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 CHAPTER 5: Automating File Management . . . . . . . . . . . . . . . . . . . . . . . . 93 Deleting Old and Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Identifying old files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Matching the file pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Sending files to the trash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Using the deletion script safely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97
📄 Page 7
Table of Contents v Backing Up Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Creating folders from Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Copying files with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Personalizing the backup script . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 Finding and Removing Duplicate Files . . . . . . . . . . . . . . . . . . . . . . . . . .103 Calculating a file hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 Finding duplicate files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 Deleting duplicate files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 Tweaking the find duplicates script . . . . . . . . . . . . . . . . . . . . . . . . .108 Compressing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 Compressing files with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112 Setting your compression parameters . . . . . . . . . . . . . . . . . . . . . . .113 Decompressing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 Unzipping files with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 Using the decompression script . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 CHAPTER 6: Automating Image and Video Files . . . . . . . . . . . . . . . . . . 119 Resizing, Rotating, Flipping, and Cropping Images . . . . . . . . . . . . . . . .120 Resizing images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 Rotating images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Flipping images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Cropping images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127 Customizing the image processor . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Converting Image File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Converting files with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131 Personalizing the conversion script . . . . . . . . . . . . . . . . . . . . . . . . .132 Extracting Frames from Video Files . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 Importing modules for video extraction . . . . . . . . . . . . . . . . . . . . .135 Looping through a video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136 Tweaking the video conversion script . . . . . . . . . . . . . . . . . . . . . . .138 CHAPTER 7: Automating Mouse and Keyboard . . . . . . . . . . . . . . . . . . . 139 Granting Permissions on a Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 Moving the Mouse, Clicking, Dragging, and Scrolling . . . . . . . . . . . . . .141 Understanding screen coordinates . . . . . . . . . . . . . . . . . . . . . . . . .141 Controlling the mouse speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 Stopping a wild mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Finding the screen locations of things . . . . . . . . . . . . . . . . . . . . . . .142 Using mouse control with a specific app . . . . . . . . . . . . . . . . . . . . .144 Trying out mouse control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 Typing Text with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Controlling the typing speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Typing long passages of text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Pressing special keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148
📄 Page 8
vi Python Automation For Dummies Pressing hotkeys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Detecting the operating system . . . . . . . . . . . . . . . . . . . . . . . . . . . .149 Detecting Keystrokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Creating Your Own Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . . .151 Automating Screenshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155 Taking screenshots with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . .157 Personalizing the auto screenshot script . . . . . . . . . . . . . . . . . . . . .158 CHAPTER 8: Automating the Office . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Automating Microsoft Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 Naming your Word document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Defining your Word content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 Automating Microsoft Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 Specifying your workbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Defining content for your workbook . . . . . . . . . . . . . . . . . . . . . . . .167 Creating and Opening PDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Defining content for your PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Identifying your PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Watermarking PDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Creating your watermark image . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 Adapting the script to your needs . . . . . . . . . . . . . . . . . . . . . . . . . . .177 PART 3: AUTOMATING THE INTERNET . . . . . . . . . . . . . . . . . . . . . . 179 CHAPTER 9: Interacting with APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Obtaining API Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 Safely Storing API Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182 Creating a .gitignore file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 Using an API key in your script . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Handling JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Parsing and serializing JSON data . . . . . . . . . . . . . . . . . . . . . . . . . . .187 Reading and writing JSON files . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Understanding REST APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190 Making API requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 Parsing API responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193 Reviewing a Complete REST API Script . . . . . . . . . . . . . . . . . . . . . . . . . .194 CHAPTER 10: Automating the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Automating Web Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 Loading drivers for your browser . . . . . . . . . . . . . . . . . . . . . . . . . . .198 Finding text boxes to fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Automating Filling Forms Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Finding a control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Submitting a form with Enter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204
📄 Page 9
Table of Contents vii Filling Multiple Text Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Clicking a form’s Submit button . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 Adapting the script to your needs . . . . . . . . . . . . . . . . . . . . . . . . . . .207 Filling Text Boxes from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208 CHAPTER 11: Scraping Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Picking the Right Tools for Web Scraping . . . . . . . . . . . . . . . . . . . . . . .213 Scraping Links from a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 Sending a browser header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215 Parsing a web page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 Extracting Data from a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 Finding elements to scrape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Scraping data from the page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 Automating Data Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Determining whether a business is open . . . . . . . . . . . . . . . . . . . .225 Scraping stock market data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 CHAPTER 12: Automating Email and Text Messages . . . . . . . . . . . . . . 229 Sending Bulk Email Automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229 Collecting account information . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Creating a .env file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Creating your email-sending script . . . . . . . . . . . . . . . . . . . . . . . . . .231 Sending HTML mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 Putting email recipient addresses in a file . . . . . . . . . . . . . . . . . . . .235 Automatically Sending Text Messages . . . . . . . . . . . . . . . . . . . . . . . . . .236 Storing SMS account information . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Defining your recipient list and message . . . . . . . . . . . . . . . . . . . . .239 Storing recipient numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 CHAPTER 13: Automating Social Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Acquiring API Keys and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 Automating Posting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 Setting up your project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245 Making the script your own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249 Creating Content for Your Posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249 Tracking Performance Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Getting Instagram API access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Setting up your script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Defining your metrics and timeframe . . . . . . . . . . . . . . . . . . . . . . .254 Analyzing Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Viewing the trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257 Setting your own keywords and timeframe . . . . . . . . . . . . . . . . . .257
📄 Page 10
viii Python Automation For Dummies PART 4: AUTOMATING MORE ADVANCED STUFF . . . . . . . . . 259 CHAPTER 14: Scheduling Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Using the Schedule Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261 Understanding how the schedule module works . . . . . . . . . . . . . .263 Scheduling tasks for intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264 Using the APScheduler Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265 Using APScheduler with intervals . . . . . . . . . . . . . . . . . . . . . . . . . . .267 Using APScheduler with dates and times . . . . . . . . . . . . . . . . . . . .268 Automating Python Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271 Running scripts as subprocesses . . . . . . . . . . . . . . . . . . . . . . . . . . .271 Running scripts as imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 CHAPTER 15: Integrating with Artificial Intelligence . . . . . . . . . . . . . . 277 Accessing Free AI through an API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277 Warming Up to a Local Chatbot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282 Installing and running Ollama . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 Downloading AI models with Ollama . . . . . . . . . . . . . . . . . . . . . . . .283 Building a simple local chatbot . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284 Creating a Conversational Chatbot . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 Developing an AI Image Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290 Showing the generated image onscreen . . . . . . . . . . . . . . . . . . . . .295 Hitting up Hugging Face . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301 PART 5: THE PART OF TENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 CHAPTER 16: Top Ten Zen of Python Guidelines . . . . . . . . . . . . . . . . . . . 311 Beautiful Is Better than Ugly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311 Explicit Is Better than Implicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Using type hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 Using comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Handling errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Simple Is Better than Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317 Complex Is Better than Complicated . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Flat Is Better than Nested . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321 Flattening nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321 Using list comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322 Sparse Is Better than Dense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 Readability Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 Special Cases Aren’t Special Enough to Break the Rule . . . . . . . . . . . .326 Practicality Beats Purity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328 Errors Should Never Pass Silently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
📄 Page 11
Table of Contents ix CHAPTER 17: Top Ten Python Error Messages . . . . . . . . . . . . . . . . . . . . . 331 Command Not Found . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331 No Module Named . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333 SyntaxError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .334 NameError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335 TypeError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336 IndexError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337 KeyError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339 AttributeError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .340 ModuleNotFoundError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342 FileNotFoundError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 IndentationError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
📄 Page 12
(This page has no text content)
📄 Page 13
Introduction 1 Introduction Welcome to Python Automation For Dummies, the book designed to help you find out all about Python and the many things you can do with Python to automate tedious, mundane, and time-consuming computer tasks. About This Book You don’t need to read this book cover-to-cover to benefit. Instead, you can treat it more like a reference for building Python automation scripts and apps as needed, just by flipping to any example in the book and using the code provided in that section. Of course, I always explain the code so you understand what’s going on. That knowledge, in turn, will grow and help you come up with your own creative solutions to automation tasks and other apps. This is not a textbook on the Python language. I don’t attempt to teach you every nook and cranny of the Python language. There are plenty of books available for that, including Python All-in-One for Dummies and Python Essentials For Dummies (published by Wiley), both of which I wrote with John Shovic. For convenience, the kinds of tasks and code you’ll use often are summed up in Part 1 of this book. When you need a quick reminder on a fundamental task, such as starting a new app from scratch, you’ll find all the steps there. Common error messages and such are covered in Chapter  17, so you can get clarification and advice in a jiffy. When something in Python leaves you stumped, use this book as a reference, paging through the table of contents or index to the spot that deals with that bit of information. Within this book, you may note that some web addresses break across two lines of text. If you’re reading this book in print and want to visit one of these web pages, simply key in the web address exactly as it’s noted in the text, pretending as though the line break doesn’t exist. If you’re reading this as an e-book, you’ve got it easy — just click the web address to be taken directly to the web page.
📄 Page 14
2 Python Automation For Dummies Foolish Assumptions This book doesn’t assume you’re already an accomplished software engineer with years of experience writing Python code. I do assume that you’re tech savvy enough to understand common tech jargon like files, folders, icons, copy, paste, gigabytes, and so forth. If I assume too much, there are plenty of resources available to you to fill in the blanks. Artificial intelligence (AI) is usually your best bet for getting quick defini- tions and answers to tech questions. Any free AI will do. In Windows, you can use Copilot. On a Mac, you can use Apple Intelligence. Or you can use any AI that includes a free tier such as ChatGPT at https://chatgpt.com. Icons Used in This Book Throughout this book, I use icons in the margin to point out content that’s perhaps a little offbeat relative to the main flow of the text. Here’s the kind of content each icon represents: The Tip icon alerts you to juicy information that makes computing easier — a time-saver or shortcut that’s worth keeping in mind, for example. Don’t forget to remember these important points because you’re likely to need the information often. These are usually items that are easy to forget but well worth remembering. When an operation could have unpleasant consequences that aren’t easily undone, the Warning icon alerts you to the dangers and tells you how to avoid that danger. Always pay attention to this icon. I use the Technical Stuff icon when I get into the weeds on a particular subject. You can ignore anything marked with this icon without missing the main point. Beyond the Book In addition to the material in the print or e-book you’re reading right now, this product also comes with a free access-anywhere Cheat Sheet that covers Python data types and keywords, arithmetic and string operators, assignment operators,
📄 Page 15
Introduction 3 and more. To get this Cheat Sheet, simply go to www.dummies.com and enter Python Automation For Dummies Cheat Sheet in the Search box. In addition, I provide all the source code for this book at www.dummies.com/go/ pythonautomationfd. Finally, at that same URL you’ll find a bonus chapter called “Managing and Speeding Up the Big Jobs.” Where to Go from Here Where you go from here is up to you! If you need a specific automation script, go to the section that describes the script you need to see all of the code for that script — not a lengthy tutorial on how to write the script. If you’re new to Python and you don’t have a lot of experience writing Python code, Chapter 1 is a great place to start.
📄 Page 16
(This page has no text content)
📄 Page 17
1Getting Started with Python Automation
📄 Page 18
IN THIS PART . . . Get Python onto your computer. Download a free editor to write Python code. Run your Python code. Master main components of Python for automation.
📄 Page 19
CHAPTER 1 Automating with Python 7 Chapter 1 Automating with Python Welcome to Python automation! In this chapter, you explore why Python is the ideal language for automating mundane, time-consuming com- puter tasks. If you’ve ever found yourself stuck doing the same boring computer chores over and over — like renaming a bazillion files, sorting through spreadsheets, or downloading stuff from the web — Python may just become your new best friend. It’s a programming language that’s easy to pick up, even if you’re not a tech wizard, and it’s perfect for automating those mind-numbing tasks that eat up your time. Think of Python as a trusty robot assistant: You tell it what to do in plain, simple words (well, code), and it does your work in no time at all. What makes Python so great for automation is the fact that it has a little some- thing for everyone. Python has built-in tools to handle all sorts of everyday tasks — like managing files, crunching data, bossing around your computer — and a huge pile of free add-ons (called libraries) can do even fancier things, like scraping websites or sending emails. You don’t need to be a coding genius to get started — just a few lines of Python can save you hours of clicking and typing. So, whether you’re organizing your music collection or taming a messy inbox, Python’s got your back, making life a whole lot easier with a few friendly commands. IN THIS CHAPTER » Deciding on a programming language » Mastering Python language basics » Getting up and running with Python
📄 Page 20
8 PART 1 Getting Started with Python Automation Choosing a Programming Language There are many programming languages in the world. They have names like C#, Go, Java, JavaScript, Python, and TypeScript, to name a few. The TIOBE Index (www.tiobe.com/tiobe-index) consistently ranks Python as the most popular language of our time. JavaScript is great for creating web apps, but it’s rarely used for anything else. Python excels at AI and automation. In fact, Python has so many ready-to-use modules designed for automation that it would probably be crazy to use any language other than Python for the kinds of automation scripts you’ll see throughout this book. THINKING LIKE A SOFTWARE ENGINEER: PUTTING ARTIFICIAL INTELLIGENCE TO WORK FOR YOU Writing Python automation requires writing computer code. This book isn’t a replace- ment for a beginner’s tutorial on the entire Python language. Instead, it’s a collection of Python automation scripts, designed to automate and simplify mundane, time- consuming computer tasks. These days, most software engineers (people who write computer code for a living) use artificial intelligence (AI) to help with writing code. Sure, you’ll hear many software engineers complain that AI can’t write code as well as they can, but that sentiment may be rooted in feeling threatened. In addition to writing code, AI can easily answer any questions that come up along the way as you’re using this book. If I throw some terminology at you that leaves you scratching your head, ask AI to explain things. If some code leaves you stymied, show the code to AI and ask it to explain the code to you. You can even tell AI to write an entire script for you, doing exactly what a script in this book does. But don’t be surprised if the code you get from AI looks different from what’s in this book. Python offers many tools and techniques for accomplishing any task. There’s no telling exactly how AI will generate code to perform some feat. If AI gives you a script that looks nothing like the script in this book, that doesn’t mean one is right and the other is wrong — you probably just have two scripts that do the same thing in different ways. That’s not unusual. They say there’s more than one way to bake a cake. Likewise, there’s more than one way to write a script to accomplish some task.
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List