Automatically renames scanned documents from Microsoft Lens (or similar apps) based on their content using AI.
The agent monitors a specified folder for files with default Microsoft Lens naming patterns (e.g., 3_28_25, 12_51 PM Microsoft Lens.jpg). When it finds such files, it analyzes their content using OpenAI's vision model (GPT-4o mini) and renames them to something more descriptive based on what's in the document.
- Automatically detects new scans with default naming
- Uses AI to analyze document content and generate meaningful filenames
- Identifies multi-page documents and names them accordingly (e.g.,
Invoice_ABC_Company_page_01.jpg) - Handles JPEG/JPG, PNG, and PDF files
- Preserves original file extensions
- Handles filename conflicts elegantly
- Can run once or as a continuous monitoring agent
- Logs all activity for troubleshooting
-
Python 3.8+ - Download Python
-
OpenAI API Key - Get it here
-
Poppler (Required for PDF support):
Windows:
- Download the latest release from poppler-windows
- Extract the downloaded file
- Move the extracted folder to
C:\Program Files\poppler(or another location) - Add the
binfolder to your PATH:- Open System Properties (Win + Pause/Break)
- Click "Advanced system settings"
- Click "Environment Variables"
- Under System Variables, find and select "Path"
- Click "Edit"
- Click "New"
- Add the path (e.g.,
C:\Program Files\poppler\Library\bin) - Click "OK" on all windows
- Restart your terminal/IDE
Mac:
brew install poppler
Linux:
sudo apt-get install poppler-utils # Ubuntu/Debian sudo yum install poppler-utils # CentOS/RHEL sudo pacman -S poppler # Arch Linux
- Run the setup script which will handle most of the setup steps:
python setup.py - Edit the created
.envfile with your OpenAI API key and folder path - Run the agent
If the automated setup doesn't work, follow these steps:
- Install Python 3.8+ if you don't already have it
- Create a virtual environment (recommended):
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate - Install dependencies:
pip install -r requirements.txt - Create a
.envfile with the following content:OPENAI_API_KEY="your_openai_api_key_here" SCAN_FOLDER_PATH="C:/Users/username/Path/To/Your/Scan/Folder/" CHECK_INTERVAL=60 CONTINUOUS_MONITORING=False - Replace the placeholder values with your actual settings
If you're having trouble with virtual environments:
-
Install the required packages directly:
pip install openai python-dotenv pillow watchdog -
Create a
.envfile in the same folder as the script with:OPENAI_API_KEY="your_openai_api_key_here" SCAN_FOLDER_PATH="C:/Users/username/Path/To/Your/Scan/Folder/" CHECK_INTERVAL=60 CONTINUOUS_MONITORING=False -
Run the agent:
python scan_agent.py
-
Install Microsoft Lens:
-
Configure OneDrive sync:
- Open Microsoft Lens
- Sign in with your Microsoft account
- Go to Settings > Cloud Storage
- Enable OneDrive sync
- Choose or create a folder for your scans (remember this path for later)
-
Scanning Best Practices:
- Use good lighting
- Keep the camera steady
- Ensure the document is flat and fully visible
- For multi-page documents, use the "Add New" button between pages
- Choose "Document" mode for text documents
- Use "Photo" mode for images or colorful content
- Use "Whiteboard" mode for whiteboards or flipcharts
You can use any scanning app that saves to a monitored folder. Some alternatives:
- Adobe Scan
- Scanner Pro
- Your phone's built-in document scanner
- Install OneDrive on your computer:
- Sign in with the same Microsoft account used in Microsoft Lens
- Ensure the scan folder is synced to your computer
- Use this local sync folder path in your
.envconfiguration
- Visit OpenAI's Platform website
- Click "Sign Up" or "Log In"
- Go to API Keys section
- Click "Create new secret key"
- Copy the key (you won't be able to see it again!)
- Add the key to your
.envfile
Note: OpenAI API usage is not free, but the cost for renaming documents is typically very low (a few cents or less per document).
To scan and process all files in the folder once:
python scan_agent.py
To reprocess all files, even if they've been processed before:
python scan_agent.py --force
To continuously monitor for new files, set CONTINUOUS_MONITORING=True in your .env file, then run:
python scan_agent.py
Or use the command line flag to override the setting in the .env file:
python scan_agent.py --continuous
The following command line options are available:
--force,-f: Force reprocessing of all matching files, even if they've been processed before--continuous,-c: Run in continuous monitoring mode (overrides .env setting)--once,-o: Run once and exit (overrides .env setting)
The agent automatically detects when multiple image files are scanned in sequence (within 60 seconds of each other) and treats them as pages of the same document. The images will be renamed with a consistent base name plus page numbers:
For example, if you scan multiple pages of an invoice as separate image files, they will be renamed to:
Invoice_ABC_Company_page_01.jpgInvoice_ABC_Company_page_02.jpg- etc.
Only files with the same extension (all JPGs or all PNGs) will be grouped together.
PDFs are always treated as standalone documents, since they can already contain multiple pages. Each PDF is analyzed and renamed individually based on its content, regardless of when it was created.
For PDFs, the agent converts only the first page to an image for analysis by the AI model, then uses the resulting name suggestion for the entire PDF file.
Note: PDF support requires Poppler to be installed. See the Prerequisites section above for installation instructions.
Edit the .env file to configure:
OPENAI_API_KEY: Your OpenAI API keySCAN_FOLDER_PATH: Path to the folder containing your scans (use forward slashes)CHECK_INTERVAL: How often to check for new files (in seconds) when in continuous modeCONTINUOUS_MONITORING: Set toTrueto run continuously,Falsefor one-time run
- Python 3.8+
- OpenAI API key
- Dependencies listed in
requirements.txt
To enable PDF processing, you also need Poppler:
- Windows: Download from here and add the
binfolder to your PATH - Mac:
brew install poppler - Linux:
apt-get install poppler-utils
PDF processing converts the first page of each PDF to an image before sending to the OpenAI API.