Reflect exports, assets like images and PDFs under a secure link. This script will search through your exported markdown files, find these asset links, and download the images for you.
import os
import re
import requests
from pathlib import Path
import zipfile
import shutil
def extract_image_links(markdown_content):
# Regex pattern to find image links in markdown
image_link_pattern = re.compile(r'!\[.*?\]\((.*?)\)')
return image_link_pattern.findall(markdown_content)
def download_image(url, save_path):
# Replace \& with & in URLs
url = url.replace(r'\&', '&')
response = requests.get(url)
if response.status_code == 200:
with open(save_path, 'wb') as file:
file.write(response.content)
else:
print(f"Failed to download image from {url}")
def process_markdown_files(folder_path, temp_folder):
# Create temporary directory if it doesn't exist
Path(temp_folder).mkdir(parents=True, exist_ok=True)
# Walk through all files in the directory
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as markdown_file:
content = markdown_file.read()
image_links = extract_image_links(content)
for i, url in enumerate(image_links):
# Extract file extension from URL if possible
extension = Path(url).suffix or '.png'
image_name = f"{os.path.splitext(file)[0]}_image_{i + 1}{extension}"
save_path = os.path.join(temp_folder, image_name)
download_image(url, save_path)
print(f"Downloaded {url} to {save_path}")
def create_zip_file(output_folder, zip_file_path):
# Create a zip file from the contents of the temporary directory
with zipfile.ZipFile(f"{zip_file_path}.zip", 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(output_folder):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, output_folder)
zipf.write(file_path, arcname)
print(f"Added {file_path} to zip file as {arcname}")
print(f"Created zip file at {zip_file_path}.zip")
if __name__ == "__main__":
folder_path = "/path/to/your/unzipped/folder" # Replace with the path to your unzipped folder
temp_folder = "/path/to/your/temporary/folder" # Temporary folder for downloaded images
zip_file_path = "/path/to/your/output/zipfile" # Path for the output zip file
process_markdown_files(folder_path, temp_folder)
create_zip_file(temp_folder, zip_file_path)
# Clean up the temporary folder
shutil.rmtree(temp_folder)
Install Required Libraries: Ensure you have requests installed. If not, you can install it using pip install requests.
Set Paths: Replace "path/to/unzipped/folder" with the path to your unzipped folder containing markdown files. Replace "path/to/output/folder" with the path where you want the images to be saved.
Run the Script: Save the script as extract_images.py and run it using python extract_images.py.
Before extracting images, make sure you have downloaded your Reflect notes as Markdown files.
Make sure you have Python 3 installed on your Mac.
Open Terminal:
Open Terminal from Applications > Utilities.
Install Homebrew (if you don't already have it):
Homebrew is a package manager for macOS. Install it by pasting the following command into Terminal and pressing Enter:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Install Python using Homebrew:
Once Homebrew is installed, install Python by running:
brew install pythonCheck Python Installation:
In your Terminal, type:
python3 --versionYou should see a version number. If you see a message that the command is not found, you'll need to reinstall Python.
Check Pip3 Installation:
In your Terminal, type:
pip3 --versionYou should see a version number for Pip3.
If pip3 is not found, you can install it manually.
Type and enter:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.pyThen run:
python3 get-pip.pySet Up a Virtual Environment:
Navigate to your Downloads directory by using the cd command:
cd ~/DownloadsCreate a virtual environment by typing:
python3 -m venv envActivate the Virtual Environment:
Activate the virtual environment by typing:
source env/bin/activateYou should see (env) appear before your terminal prompt, indicating that the virtual environment is active.
Install Requests in the Virtual Environment:
With the virtual environment activated, install the requests library by typing:
pip install requestsCreate the Script:
Open a text editor (like TextEdit).
Copy and paste the following script into the text editor:
import os
import re
import requests
from pathlib import Path
import zipfile
import shutil
def extract_image_links(markdown_content):
# Regex pattern to find image links in markdown
image_link_pattern = re.compile(r'!\[.*?\]\((.*?)\)')
return image_link_pattern.findall(markdown_content)
def download_image(url, save_path):
# Replace \& with & in URLs
url = url.replace(r'\&', '&')
response = requests.get(url)
if response.status_code == 200:
with open(save_path, 'wb') as file:
file.write(response.content)
else:
print(f"Failed to download image from {url}")
def process_markdown_files(folder_path, temp_folder):
# Create temporary directory if it doesn't exist
Path(temp_folder).mkdir(parents=True, exist_ok=True)
# Walk through all files in the directory
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as markdown_file:
content = markdown_file.read()
image_links = extract_image_links(content)
for i, url in enumerate(image_links):
# Extract file extension from URL if possible
extension = Path(url).suffix or '.png'
image_name = f"{os.path.splitext(file)[0]}_image_{i + 1}{extension}"
save_path = os.path.join(temp_folder, image_name)
download_image(url, save_path)
print(f"Downloaded {url} to {save_path}")
def create_zip_file(output_folder, zip_file_path):
# Create a zip file from the contents of the temporary directory
with zipfile.ZipFile(f"{zip_file_path}.zip", 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(output_folder):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, output_folder)
zipf.write(file_path, arcname)
print(f"Added {file_path} to zip file as {arcname}")
print(f"Created zip file at {zip_file_path}.zip")
if __name__ == "__main__":
folder_path = "/path/to/your/unzipped/folder" # Replace with the path to your unzipped folder
temp_folder = "/path/to/your/temporary/folder" # Temporary folder for downloaded images
zip_file_path = "/path/to/your/output/zipfile" # Path for the output zip file
process_markdown_files(folder_path, temp_folder)
create_zip_file(temp_folder, zip_file_path)
# Clean up the temporary folder
shutil.rmtree(temp_folder)
Save the Script:
Save the file as extract_images.py in your Downloads directory.
Modify the Script:
Open extract_images.py in your text editor.
Find the lines near the bottom that look like this:
folder_path = "/path/to/your/unzipped/folder" # Replace with the path to your unzipped folder temp_folder = "/path/to/your/temporary/folder" # Temporary folder for downloaded images zip_file_path = "/path/to/your/output/zipfile" # Path for the output zip fileReplace "/path/to/your/unzipped/folder" with the actual path to your unzipped folder containing markdown files. For example:
folder_path = "/Users/YourUsername/Downloads/UnzippedMarkdown"Replace "/path/to/your/temporary/folder" with a temporary folder path. For example:
temp_folder = "/Users/YourUsername/Downloads/DownloadedImagesTemp"Replace "/path/to/your/output/zipfile" with the desired output zip file path. For example:
zip_file_path = "/Users/YourUsername/Downloads/DownloadedImages"Open Terminal:
Open Terminal from Applications > Utilities.
Activate the Virtual Environment:
Navigate to your Downloads directory:
cd ~/DownloadsActivate the virtual environment:
source env/bin/activateNavigate to the Script Location:
In your Terminal, navigate to the directory where you saved extract_images.py. If you saved it in your Downloads folder, you are already there.
Run the Script:
Execute the script by typing:
python3 extract_images.pyThe script will process the markdown files in the specified folder.
It will download the images to a temporary directory.
It will create a zip file of the downloaded images.
The zip file will be saved to the specified path (e.g., /Users/YourUsername/Downloads/DownloadedImages.zip).
The temporary directory will be deleted after the zip file is created.
Python Not Found: If your terminal says python3 is not recognized, you might need to check your Python installation.
Permissions Issues: Ensure you have the necessary permissions to create and write files in the specified directories.
Network Issues: Ensure your internet connection is stable, as the script needs to download images from the web.
By following these steps, you should be able to run the script successfully and download images from your markdown files into a single zip file. If you encounter any issues, refer to the troubleshooting section or seek additional help.