Created
April 17, 2016 21:13
-
-
Save DocumentAlchemy/fb8d34c404ccb661a90f4f99655fabbe to your computer and use it in GitHub Desktop.
Shell script that converts an old-style *.doc Microsoft Word document into a modern *.docx MS Word document using the DocumentAlchemy document conversion API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Converts one or more *.doc files into *.docx | |
# USAGE: doc2docx <DOC_FILE> | |
# doc2docx <DIRECTORY>/*.doc | |
# Errors are reported, but do not stop further processing. | |
# Exits with `0` if the given files were converted correctly, | |
# or with the number of failing files. | |
# This is your DocumentAlchemy API key. It may be set via the | |
# environment variable named `DA_API_KEY`. | |
# (See <https://documentalchemy.com/> to get one of your own.) | |
API_KEY=${DA_API_KEY:-"403l1zh3dkbakyb9"} | |
# This is a flag that can prevent this script from `echo`ing | |
# unnecessary information. It can be set via the environment | |
# variable `QUIET`. | |
# A clever person could make this into a command line parameter | |
# like `-q`. | |
QUIET=${QUIET:-FALSE} | |
# Note that you can set environment variables on a per-invocation | |
# basis by prefixing NAME=VALUE before the command. For example: | |
# API_KEY=my-key QUIET=true doc2docx *.doc | |
# EXIT_CODE tracks the number of input documents we couldn't convert. | |
EXIT_CODE=0 | |
# Loop over the command line parameters.... | |
for doc in "$@"; do | |
# ...testing that is is an accessible file... | |
if ! [ -s "$doc" ]; then | |
$QUIET || echo "WARNING: File '$doc' was not found and will be ignored." | |
EXIT_CODE=$((EXIT_CODE+1)) | |
else | |
# ...and that the filename ends with `.doc`... | |
docx="`echo "$doc" | sed 's/\.doc$/\.docx/'`" | |
if [[ "$doc" == "$docx" ]]; then | |
$QUIET || echo "WARNING File '$doc' does not end in '.doc' and will be ignored." | |
EXIT_CODE=$((EXIT_CODE+1)) | |
else | |
# ...if so, POST to DocumentAlchemy to convert the file... | |
$QUIET || echo "Converting '$doc' into '`basename "$docx"`'..."; | |
response=$(curl --silent \ | |
--write-out %{http_code} -H "Authorization: da.key=$API_KEY" \ | |
-X POST --form "document=@$doc" \ | |
https://documentalchemy.com/api/v1/document/-/rendition/docx \ | |
-o "$docx") | |
# ...and report success or failure. | |
if ! [ "$response" -eq "200" ]; then | |
$QUIET || echo "WARNING: Expected a 200 response for file '$doc', found $response instead."; | |
EXIT_CODE=$((EXIT_CODE+1)) | |
else | |
$QUIET || echo "...OK. File '$docx' created." | |
fi | |
fi | |
fi | |
done | |
# Exit with the number of documents that could not be converted. | |
exit $EXIT_CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment