Skip to content

Instantly share code, notes, and snippets.

@Sarquamon
Last active July 12, 2026 15:06
Show Gist options
  • Select an option

  • Save Sarquamon/a399dbca5e12ed4cd5ed00bd6fd846b3 to your computer and use it in GitHub Desktop.

Select an option

Save Sarquamon/a399dbca5e12ed4cd5ed00bd6fd846b3 to your computer and use it in GitHub Desktop.
Small bash file for transforming pdf files into markdown file leveraging on markitdown from microsoft and python's venv
#!/bin/bash
# Bash script responsible for transforming pdf files to md files for LLMs
VENV_PATH="/Users/salo/Documents/personal/repos/customMarkItDown/python_virtual_env"
# Extract filename with extension (e.g., "file.pdf")
stripFilenameFromInputFilePath() {
local src="$1"
echo "${src##*/}"
}
# Extract the directory path (e.g., "/path/to")
stripPathFromInputFilePath() {
local src="$1"
if [[ "$src" == */* ]]; then
echo "${src%/*}"
else
echo "."
fi
}
# Extract filename without extension (e.g., "file" from "file.pdf")
stripExtension() {
local filename="$1"
echo "${filename%.*}"
}
activateVenv() {
source "$VENV_PATH/bin/activate"
}
callMarkItDown() {
local input_file="$1"
local output_file="$2"
markitdown "$input_file" > "$output_file"
}
deactivateVenv() {
deactivate
}
transform() {
local input="$1"
local custom_output="$2"
local output_target=""
if [ -n "$custom_output" ]; then
output_target="$custom_output"
else
local filepath=$(stripPathFromInputFilePath "$input")
local filename_with_ext=$(stripFilenameFromInputFilePath "$input")
local filename_no_ext=$(stripExtension "$filename_with_ext")
output_target="$filepath/$filename_no_ext.md"
fi
echo "Converting: $input -> $output_target"
# The actual work
callMarkItDown "$input" "$output_target"
}
# --- Main Execution ---
if [ $# -eq 0 ]; then
echo "Usage:"
echo " Single file with custom output: pdf2md <input.pdf> [output.md]"
echo " Batch processing: pdf2md <file1.pdf> <file2.pdf> ..."
echo " Wildcard processing: pdf2md *.pdf"
exit 1
fi
activateVenv
# Check if there are exactly 2 arguments AND the second one is NOT a PDF.
# *.[pP][dD][fF] catches .pdf, .PDF, .Pdf, etc.
if [ $# -eq 2 ] && [[ "$2" != *.[pP][dD][fF] ]]; then
transform "$1" "$2"
else
# Loop through all arguments passed to the script
for file in "$@"; do
# Only process if the file actually exists and is a PDF
if [ -f "$file" ] && [[ "$file" == *.[pP][dD][fF] ]]; then
transform "$file" ""
else
echo "Skipping: $file (Not a valid PDF)"
fi
done
fi
deactivateVenv
echo "Done!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment