Bulk Print Word Documents with Python: Automate Your Office Workflow

If you work in an administrative role, you know the pain. You have a folder with 50 generated .docx files (contracts, invoices, or legal rulings), and you need to print them all.

Opening them one by one, pressing Ctrl+P, and waiting for Word to load is a waste of your life.

In this guide, I will show you how to build a simple Python script using AI that sends an entire folder of documents to your printer automatically in the background.

Stop printing files one by one. Learn how to use Python (win32print) and AI to build a script that bulk prints hundreds of .docx files instantly



The Secret Library: Win32com

To talk to a printer on Windows, Python needs the pywin32 library. It allows Python to control Microsoft Word directly.

First, install it:

pip install pywin32

Step 1: Asking AI for the Logic

Interacting with Windows API is tricky. You need to handle the "Default Printer" selection. Instead of reading boring documentation, let's ask AI.

The Prompt:
"Write a Python script using win32com.client to print all .docx files in a specific folder. The script should:
1. Open MS Word in the background (invisible).
2. Loop through files in a folder.
3. Send 'PrintOut' command to the default printer.
4. Close the document without saving changes."

Step 2: The Script (Copy This)

Here is the robust script generated by AI. Save this as autoprint.py.

import os
import win32com.client
import time

# Configuration
SOURCE_FOLDER = r"C:\Users\Admin\Documents\ToPrint"

def print_files(folder):
    # Create an instance of Word Application
    word = win32com.client.Dispatch("Word.Application")
    word.Visible = False  # Run in background
    
    files = [f for f in os.listdir(folder) if f.endswith(".docx")]
    
    if not files:
        print("No DOCX files found!")
        return

    print(f"Found {len(files)} files. Starting print job...")

    for filename in files:
        filepath = os.path.join(folder, filename)
        try:
            doc = word.Documents.Open(filepath)
            doc.PrintOut()
            doc.Close(False) # Close without saving
            print(f"Sent to printer: {filename}")
            time.sleep(2) # Pause to prevent jamming
        except Exception as e:
            print(f"Error printing {filename}: {e}")

    word.Quit()
    print("All jobs sent to printer.")

if __name__ == "__main__":
    print_files(SOURCE_FOLDER)

Step 3: Handling "Printer Not Found" Errors

Sometimes, Python cannot find the printer if the name is complicated (like network printers). If you get an error, ask AI to write a "Printer Selector" script.

Prompt: "Python script to list all available printers using win32print and let user select one."

Conclusion

Automation isn't just about digital data. With Python, you can control physical hardware like printers. Use this script to save hours of manual clicking every week.

Warning: Always test with 1-2 files first before printing 100 pages, or you might run out of ink!


Author: Marg | Daily Innovate Tech

Post a Comment for "Bulk Print Word Documents with Python: Automate Your Office Workflow"