Skip to content

Instantly share code, notes, and snippets.

@abathur
Created February 5, 2022 22:55
Show Gist options
  • Save abathur/a3ae3ceeb0b9d3f8d6b2741b3b10ee61 to your computer and use it in GitHub Desktop.
Save abathur/a3ae3ceeb0b9d3f8d6b2741b3b10ee61 to your computer and use it in GitHub Desktop.

Revisions

  1. abathur created this gist Feb 5, 2022.
    501 changes: 501 additions & 0 deletions pdf2odt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,501 @@
    #!/nix/store/z6rd8wq02azalrlm2m5k08iy53klg624-bash-5.1-p12/bin/bash

    # Usage: pdf2{odt,ods} [ options ] input.{jpg,pdf,png} ... output.{odt,ods}
    #
    # This script converts one or more PDF, JPG, or PNG files to an ODT or ODS
    # file. The contents of any PDF file(s) is first converted to a set of image
    # files. These files are then inserted as background images in the ODT or
    # ODS file.
    #
    # Copyright (c) 2011 Markus Gutschke. All rights reserved.
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to
    # deal in the Software without restriction, including without limitation the
    # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    # sell copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    #
    # The above copyright notice and this permission notice shall be included in all
    # copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    # IN THE SOFTWARE.

    export LC_ALL=C

    # Check arguments
    [ $# -ge 2 ] || {
    echo "Usage:" >&2
    echo " $(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/basename "$0") [ options ] input.{jpg,pdf,png} ... output.{odt,ods}" >&2
    echo >&2
    echo "Options:" >&2
    echo " -- Stop scanning for options." >&2
    echo " --format={odt|ods} Select output file format." >&2
    echo " --margin=X{cm|in} Add extra margin around input files." >&2
    echo " --paper={letter|a4} Select paper size." >&2
    echo " --portrait Portrait is always the default orientation." >&2
    echo " --landscape Assume all input files are in landscape format." >&2
    echo " --resolution=RES Change resolution of embedded images." >&2
    echo " Only affects PDF files. PDF/PNG files will not" >&2
    echo " be rescaled." >&2
    echo " --trim|--no-trim Attempt to trim existing margins from input files." >&2
    exit 1
    }

    # Attempt to deduce the output file format from the output file name.
    eval out="\${$#}"
    format="${out##*.}"; format="$(printf '%s' "${format}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)"
    [ "x${format}" = "xodt" -o "x${format}" = "xods" ] || format="${0: -3}"
    [ "x${format}" = "xodt" -o "x${format}" = "xods" ] || format="odt"

    # Select default papersize.
    papersize="${PAPERSIZE:-$(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat "${PAPERCONF:-/etc/papersize}" 2>/dev/null || :)}"
    [ "x${papersize}" != "a4" ] && papersize="letter"

    # Parse position-independent command line flags.
    pdf=("$@"); unset pdf[$((${#pdf[@]}-1))]
    margin="0in"
    orientation="portrait"
    i=0; while [ ${i} -lt ${#pdf[@]} ]; do
    if [ "x${pdf[${i}]#-}" != "x${pdf[${i}]}" ]; then
    case "${pdf[${i}]}" in
    --)
    break
    ;;
    --format|--format=*)
    if [ "x${pdf[${i}]#--format=}" != "x${pdf[${i}]}" ]; then
    format="${pdf[${i}]#--format=}"
    else
    i=$((${i} + 1))
    format="${pdf[${i}]}"
    fi
    format="$(printf '%s' "${format}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)"
    if [ "x${format}" != "xodt" -a "x${format}" != "xods" ]; then
    echo "Unsupported output format \"${format}\"." >&2
    exit 1
    fi
    ;;
    --margin|--margin=*)
    if [ "x${pdf[${i}]#--margin=}" != "x${pdf[${i}]}" ]; then
    margin="${pdf[${i}]#--margin=}"
    else
    i=$((${i} + 1))
    margin="${pdf[${i}]}"
    fi
    if [ "x${margin%in}" == "x${margin}" -a \
    "x${margin%cm}" == "x${margin}" ]; then
    echo "Unsupported margin \"${margin}\" provided." >&2
    exit 1
    else
    margin="${margin/-/_}"
    fi
    ;;
    --paper|--paper=*)
    if [ "x${pdf[${i}]#--paper=}" != "x${pdf[${i}]}" ]; then
    papersize="${pdf[${i}]#--paper=}"
    else
    i=$((${i} + 1))
    papersize="${pdf[${i}]}"
    fi
    if [ "x${papersize}" != "xletter" -a "x${papersize}" != "xa4" ]; then
    echo "Unsupported paper size \"${papersize}\" provided." >&2
    exit 1
    fi
    ;;
    --portrait)
    orientation="portrait"
    ;;
    --landscape)
    orientation="landscape"
    ;;
    --resolution=*)
    ;;
    --resolution)
    i=$((${i} + 1))
    ;;
    --trim|--no-trim)
    ;;
    *)
    echo "Unknown command line option \"${pdf[${i}]}\"." >&2
    exit 1
    ;;
    esac
    else
    if [ ! -r "${pdf[${i}]}" ]; then
    echo "Invalid or non-existent filename \"${pdf[${i}]}\"." >&2
    exit 1
    fi
    fi
    i=$((${i} + 1))
    done
    [ "${format}" = odt ] && formatname="Text" || formatname="Spreadsheet"
    if [ "${papersize}" = "letter" ]; then
    units="in"
    units2inch="1"
    width="8.5"
    height="11"
    if [ "x${margin%in}" != "x${margin}" ]; then
    margin="${margin%in}"
    else
    margin="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${margin%cm} 2.54/p")"
    fi
    else
    units="mm"
    units2inch="25.4"
    width="210"
    height="297"
    if [ "x${margin%cm}" != "x${margin}" ]; then
    margin="${margin%cm}"
    else
    margin="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${margin%in} 2.54*p")"
    fi
    fi
    if [ "${orientation}" != "portrait" ]; then
    w="${width}"
    width="${height}"
    height="${w}"
    fi

    # Don't overwrite the output file, unless it is an ODT/ODS file already.
    # This protects users from overwriting input files, if they completely
    # forgot to specify any output file.
    if [ -e "${out}" ] &&
    [[ ! "$(/nix/store/qpq3ylbybkq1dzbvksnwqmv3jr6wa20c-file-5.41/bin/file "${out}" 2>/dev/null)" =~ "OpenDocument ${formatname}" ]]; then
    echo "Did you forget to specify the output file name? \"${out}\"" \
    "exists, but is not an $(printf '%s' "${format}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr a-z A-Z) file" >&2
    exit 1
    fi

    # Set up temporary staging directory
    TMPDIR=$(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mktemp -d)

    # Adjust DPI so that the image fits on a letter- or a4-sized page.
    function scale() {
    local res="$(/nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/identify "${1}")"
    res="$(echo "x ${res#${1}}"|/nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{ print $3 }')"
    local dpiX=$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${res%x*} ${width} ${margin} 2*- ${units2inch}/0k/p")
    local dpiY=$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${res#*x} ${height} ${margin} 2*- ${units2inch}/0k/p")
    [ ${dpiX} -gt ${dpiY} ] && local dpi="${dpiX}" || local dpi="${dpiY}"
    /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert "${1}" -set units PixelsPerInch -set density "${dpi}" "${2}"
    }

    # Preprocess image file(s), and convert PDF file(s) to images
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir -p "${TMPDIR}/Pictures"
    endofargs=
    resolution=300
    trim=
    i=0; while [ ${i} -lt ${#pdf[@]} ]; do
    if [ -z "${endofargs}" -a "x${pdf[${i}]#-}" != "x${pdf[${i}]}" ]; then
    case "${pdf[${i}]}" in
    --)
    endofargs=t
    ;;
    --format=*|--margin=*|--paper=*|--portrait|--landscape)
    ;;
    --format|--margin|--paper)
    i=$((${i} + 1))
    ;;
    --resolution*)
    if [ "x${pdf[${i}]#--resolution=}" != "x${pdf[${i}]}" ]; then
    resolution="${pdf[${i}]#--resolution=}"
    else
    i=$((${i} + 1))
    resolution="${pdf[${i}]}"
    fi
    if [ "x${resolution}" != "x$(printf '%s' "${resolution}"|/nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/[^0-9]//')" ] ||
    [ "${resolution}" -lt 50 -o "${resolution}" -gt 2400 ]; then
    echo "Invalid resolution \"${resolution}\" provided." >&2
    exit 1
    fi
    ;;
    --trim)
    trim="t"
    ;;
    --no-trim)
    trim=
    ;;
    *)
    echo "Unknown command line option \"${pdf[${i}]}\"." >&2
    exit 1
    ;;
    esac
    else
    suffix="${pdf[${i}]##*.}"; suffix="$(printf '%s' "${suffix}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)"
    n=$(($(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/Image-"* 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/wc -l) + 1))
    case "$suffix" in
    png|jpg|jpeg)
    if [ "$suffix" = "jpg" ]; then suffix="jpeg"; fi
    in="${pdf[${i}]}"
    ot="${TMPDIR}/Pictures/Image-$(printf '%03d' $n).$suffix"
    [ -n "${trim}" ] && { /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert "${in}" "${ot}"; in="${ot}"; }
    scale "${in}" "${ot}"
    ;;
    *)
    /nix/store/w0k1ip6cizl1a8lf6sxvynf504xzdjw3-ghostscript-9.55.0/bin/gs -dNOPAUSE -sDEVICE=png256 \
    -sOutputFile="${TMPDIR}/Pictures/TMP-%03d.png" \
    -q -r"${resolution}" -dBATCH "${pdf[${i}]}"
    for j in "${TMPDIR}/Pictures/TMP-"*".png"; do
    in="${j}"
    ot="${TMPDIR}/Pictures/Image-$(printf '%03d' $n).png"
    [ -n "${trim}" ] && { /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert "${in}" "${ot}"; in="${ot}"; }
    if [ "${margin}" != "0" ]; then
    scale "${in}" "${ot}"
    [ "${in}" != "${ot}" ] && /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -f "${in}"
    elif [ "${in}" != "${ot}" ]; then
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mv "${in}" "${ot}"
    fi
    n=$((${n} + 1))
    done
    ;;
    esac
    fi
    i=$((${i} + 1))
    done
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir -p "${TMPDIR}/META-INF"

    # Create "mimetype" file
    printf '%s' "application/vnd.oasis.opendocument.$(printf '%s' "${formatname}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)" >"${TMPDIR}/mimetype"

    # Create "META-INF/manifest.xml" file
    { /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
    <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.$(printf '%s' "${formatname}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)" manifest:version="1.2" manifest:full-path="/"/>
    <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
    <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/>
    EOF
    [ "${format}" = "ods" ] &&
    echo ' <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/>'
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \
    /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/\([^.]*[.]\)\(.*\)/ <manifest:file-entry manifest:media-type="image\/\2" manifest:full-path="Pictures\/\1\2/;s/$/"\/>/'
    echo '</manifest:manifest>'; } >"${TMPDIR}/META-INF/manifest.xml"

    # Create "content.xml" file
    if [ "${format}" = "odt" ]; then
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    <?xml version="1.0" encoding="UTF-8"?>
    <office:document-content
    xmlns:grddl="http://www.w3.org/2003/g/data-view#"
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl"
    office:version="1.2">
    <office:automatic-styles>
    EOF
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \
    /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.].*/ <style:style style:name="Image\1" style:family="paragraph" style:master-page-name="Image\1"><style:paragraph-properties style:page-number="auto"\/><\/style:style>/'
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    </office:automatic-styles>
    <office:body>
    <office:text text:use-soft-page-breaks="true">
    EOF
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \
    /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.].*/ <text:p text:style-name="Image\1"\/>/'
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    </office:text>
    </office:body>
    </office:document-content>
    EOF
    else
    if [ "${papersize}" = "letter" ]; then
    xcs="0.03125" # 1/32"
    ycs="0.03125" # 1/32"
    ucs="in"
    u2i="1"
    else
    xcs="0.75"
    ycs="0.75"
    ucs="mm"
    u2i="25.4"
    fi
    wcs="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${width} ${units2inch}/ ${xcs} ${u2i}/0k/p")"
    hcs="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${height} ${units2inch}/ ${ycs} ${u2i}/0k/p")"
    col="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "${wcs} [1 - d 26 % 65 + a r 26 / d 0 !=a] sa lax sa [n z 0 !=a]sa lax")"
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <office:document-content
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0"
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    office:version="1.2">
    <office:automatic-styles>
    <style:style style:name="co1" style:family="table-column">
    <style:table-column-properties style:column-width="${xcs}${ucs}"/>
    </style:style>
    <style:style style:name="ro1" style:family="table-row">
    <style:table-row-properties style:row-height="${ycs}${ucs}"/>
    </style:style>
    </office:automatic-styles>
    <office:body>
    <office:spreadsheet>
    EOF
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \
    while read -r file; do
    idx="${file#${TMPDIR}/Pictures/Image-}"; idx="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "${idx%.*}p")"
    res="$(/nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/identify "${file}")"
    res="$(echo "x ${res#${file}}"|/nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{ print $3 }')"
    trim='/[.]/b1;q;:1;s/\([1-9]\)0*$/\1/;s/[.]0*$//'
    round=' 8k1/0.00000005+6k1/'
    iwu="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${height} ${res#*x}/${res%x*}*ddsa ${width} dsb [lb]sc<c ${units2inch}/${u2i}*${round}p"|/nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed "${trim}")"
    ihu="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${width} ${res%x*}/${res#*x}*ddsa ${height}dsb [lb]sc<c ${units2inch}/${u2i}*${round}p"|/nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed "${trim}")"
    ilo="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${width} ${units2inch}/${u2i}* ${iwu}- 2/${round}p"|/nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed -e "${trim}")"
    ito="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${height} ${units2inch}/${u2i}* ${ihu}- 2/${round}p"|/nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed -e "${trim}")"
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<EOF
    <table:table table:name="Sheet${idx}" table:print-ranges="Sheet${idx}.A1:Sheet${idx}.${col}${hcs}">
    <table:shapes>
    <draw:frame draw:z-index="0" draw:name="Image ${idx}" table:table-background="true" svg:width="${iwu}${ucs}" svg:height="${ihu}${ucs}" svg:x="${ilo}${ucs}" svg:y="${ito}${ucs}">
    <draw:image xlink:href="${file#${TMPDIR}/}" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad">
    </draw:image>
    </draw:frame>
    </table:shapes>
    <table:table-column table:style-name="co1" table:number-columns-repeated="${wcs}"/>
    <table:table-row table:style-name="ro1" table:number-rows-repeated="$((hcs-1))">
    <table:table-cell table:number-columns-repeated="${wcs}"/>
    </table:table-row>
    <table:table-row table:style-name="ro1">
    <table:table-cell table:number-columns-repeated="$((wcs-1))"/>
    <table:table-cell office:value-type="string" calcext:value-type="string">
    <text:p><text:s/></text:p>
    </table:table-cell>
    </table:table-row>
    </table:table>
    EOF
    done
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    </office:spreadsheet>
    </office:body>
    </office:document-content>
    EOF
    fi >"${TMPDIR}/content.xml"

    # Create "styles.xml" file
    if [ "${format}" = "odt" ]; then
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    <?xml version="1.0" encoding="UTF-8"?>
    <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    xmlns:grddl="http://www.w3.org/2003/g/data-view#"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl"
    office:version="1.2">
    <office:automatic-styles>
    EOF
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \
    /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.]\(.*\)/ <style:page-layout style:name="Image\1"><style:page-layout-properties fo:page-width="'"${width}${units}"'" fo:page-height="'"${height}${units}"'" style:num-format="1" style:print-orientation="'"${orientation}"'" fo:margin-top="0in" fo:margin-bottom="0in" fo:margin-left="0in" fo:margin-right="0in" fo:background-color="transparent" style:writing-mode="lr-tb" style:footnote-max-height="0in"><style:background-image xlink:href="Pictures\/Image-\1.\2" xlink:type="simple" xlink:actuate="onLoad" style:position="center center" style:repeat="no-repeat"\/><\/style:page-layout-properties><\/style:page-layout>/'
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    </office:automatic-styles>
    <office:master-styles>
    EOF
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \
    /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.].*/ <style:master-page style:name="Image\1" style:page-layout-name="Image\1"\/>/'
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF'
    </office:master-styles>
    </office:document-styles>
    EOF
    else
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    office:version="1.2">
    <office:styles>
    <style:default-style style:family="graphic">
    <style:paragraph-properties style:font-independent-line-spacing="true"/>
    <style:text-properties style:font-family-generic="roman" style:font-pitch="variable" fo:font-size="0pt"/>
    </style:default-style>
    </office:styles>
    <office:automatic-styles>
    <style:page-layout style:name="PageLayout">
    <style:page-layout-properties fo:margin-top="0in" fo:margin-bottom="0in" fo:margin-left="0in" fo:margin-right="0in" fo:page-width="${width}${units}" fo:page-height="${height}${units}" style:scale-to-pages="1"/>
    <style:header-style>
    <style:header-footer-properties fo:min-height="0in" fo:margin-left="0in" fo:margin-right="0in" fo:margin-bottom="0in"/>
    </style:header-style>
    <style:footer-style>
    <style:header-footer-properties fo:min-height="0in" fo:margin-left="0in" fo:margin-right="0in" fo:margin-top="0in"/>
    </style:footer-style>
    </style:page-layout>
    </office:automatic-styles>
    <office:master-styles>
    <style:master-page style:name="Default" style:page-layout-name="PageLayout">
    <style:header style:display="false"/>
    <style:header-left style:display="false"/>
    <style:footer style:display="false"/>
    <style:footer-left style:display="false"/>
    </style:master-page>
    </office:master-styles>
    </office:document-styles>
    EOF
    fi >"${TMPDIR}/styles.xml"

    # Create "settings.xml" file
    if [ "${format}" = "ods" ]; then
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat >>"${TMPDIR}/settings.xml" <<'EOF'
    <?xml version="1.0" encoding="UTF-8"?>
    <office:document-settings
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
    xmlns:ooo="http://openoffice.org/2004/office"
    office:version="1.2">
    <office:settings>
    <config:config-item-set config:name="ooo:view-settings">
    <config:config-item-map-indexed config:name="Views">
    <config:config-item-map-entry>
    <config:config-item config:name="ShowGrid" config:type="false">true</config:config-item>
    </config:config-item-map-entry>
    </config:config-item-map-indexed>
    </config:config-item-set>
    </office:settings>
    </office:document-settings>
    EOF
    fi

    # Pack individual files into ODT/ODS archive
    [ "x${out}" = "x${out#/}" ] && out="${PWD}/${out}" || :
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -f "${out}"
    cd "${TMPDIR}" &&
    /nix/store/qziw36maxmgng6rvcrni0x4qylygbzyy-zip-3.0/bin/zip -m -q -X -0 "${out}" mimetype &&
    /nix/store/qziw36maxmgng6rvcrni0x4qylygbzyy-zip-3.0/bin/zip -m -q -X "${out}" $(/nix/store/9amg9w74w2qch736hjbwgi35fhmzfvyp-findutils-4.8.0/bin/find . -name mimetype -o -type f -print) &&
    cd ..

    # Clean up
    /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -rf "${TMPDIR}"
    trap '' EXIT INT TERM QUIT HUP

    # Done
    exit 0

    ### resholve directives (auto-generated) ## format_version: 2
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/basename
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mktemp
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mv
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr
    # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/wc
    # resholve: keep /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert
    # resholve: keep /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/identify
    # resholve: keep /nix/store/9amg9w74w2qch736hjbwgi35fhmzfvyp-findutils-4.8.0/bin/find
    # resholve: keep /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed
    # resholve: keep /nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc
    # resholve: keep /nix/store/qpq3ylbybkq1dzbvksnwqmv3jr6wa20c-file-5.41/bin/file
    # resholve: keep /nix/store/qziw36maxmgng6rvcrni0x4qylygbzyy-zip-3.0/bin/zip
    # resholve: keep /nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk
    # resholve: keep /nix/store/w0k1ip6cizl1a8lf6sxvynf504xzdjw3-ghostscript-9.55.0/bin/gs