converting attachments to text

Bart Bunting bart.bunting at ursys.com.au
Mon Jan 9 16:57:54 PST 2017


Hi Brian,
Thanks so much for this, it has done exactly what I was after!


Kind regards

Bart

Brian Sniffen <bts at evenmere.org> writes:

> Sure!  Here's what I use for docx, and I think it could be adapted to
> pdf with pdftotext or whatever you're already using there.  You need a
> small shell script that reads from STDIN, writes to a file, and calls
> pandoc or pdftotext or whatever, like ~/bin/antiwordx:
>
>     #!/bin/sh
>
>     tmpfile=$(mktemp /tmp/antiwordx.XXXXXX.docx)
>     trap 'rm -f -- "$tmpfile"' INT TERM HUP EXIT
>     cat > "$tmpfile"
>     pandoc --normalize -r docx -w markdown "$tmpfile"
>
> You need a small handler function to call it from Elisp---see attached
> file `inline-docx.el`, which assumed you have both the old `antiword`
> for old-style .doc files and pandoc for new-style `docx`.
>
> I apologize for the roughness of the code; it should probably use
> customizable paths for pandoc and such.
>
> -Brian
>
>
> (defun mm-inline-msword (handle)                                                  
>   (let (text)                                                                     
>     (with-temp-buffer                                                             
>       (mm-insert-part handle)                                                     
>       (call-process-region (point-min) (point-max) "antiword" t t nil "-")        
>       (setq text (buffer-string)))                                                
>     (mm-insert-inline handle text)))                                              
>                                                                                   
> (defun mm-inline-docx (handle)
>   "pandoc --normalize -r docx -w markdown %s"
>   (let (text)                                                                     
>     (with-temp-buffer                                                             
>       (mm-insert-part handle)                                                     
>       (let ((coding-system-for-read 'utf-8))
> 	(call-process-region (point-min) (point-max) "/Users/bts/bin/antiwordx" t t nil))
>       (setq text (buffer-string)))                                                
>     (mm-insert-inline handle text)))                                              
>
> (setq my-inline-mime-tests
>      '(("text/rtf" mm-inline-rtf
>         (lambda
>           (handle)
>           (let
>               ((name
>                 (mail-content-type-get
>                  (mm-handle-disposition handle)
>                  'filename)))
>             (and name
>                  (equal ".rtf"
>                         (substring name -4 nil))))))
>        ("application/x-msword" mm-inline-docx
>         (lambda
>           (handle)
>           (let
>               ((name
>                 (mail-content-type-get
>                  (mm-handle-disposition handle)
>                  'filename)))
>             (and name
>                  (equal ".docx"
>                         (substring name -5 nil))))))
>        ("application/x-msword" mm-inline-msword
>         (lambda
>           (handle)
>           (let
>               ((name
>                 (mail-content-type-get
>                  (mm-handle-disposition handle)
>                  'filename)))
>             (and name
>                  (equal ".doc"
>                         (substring name -4 nil))))))
>        ("application/vnd.openxmlformats-officedocument.wordprocessingml.document" mm-inline-docx identity)
>        ("application/octet-stream" mm-inline-docx
>         (lambda
>           (handle)
>           (let
>               ((name
>                 (mail-content-type-get
>                  (mm-handle-disposition handle)
>                  'filename)))
>             (and name
>                  (equal ".docx"
>                         (substring name -5 nil))))))
>        ("application/octet-stream" mm-inline-msword
>         (lambda
>           (handle)
>           (let
>               ((name
>                 (mail-content-type-get
>                  (mm-handle-disposition handle)
>                  'filename)))
>             (and name
>                  (equal ".doc"
>                         (substring name -4 nil))))))
>        ("application/msword" mm-inline-msword identity)))
>
> (mapcar (lambda (x) (add-to-list 'mm-inlined-types (car x)))
>         my-inline-mime-tests)
>
> (mapcar (lambda (x) (add-to-list 'mm-inline-media-tests x))
>         my-inline-mime-tests)
>
>
> Bart Bunting <bart.bunting at ursys.com.au> writes:
>
>> Hi,
>>
>> Just looking for some pointers.
>>
>> I have to deal with quite a few emails with attachments in either pdf or
>> word format.
>>
>> I'm on a mac so can use applescript or something pdftotext or similar to
>> convert them to text.
>>
>> I'm blind so use emacspeak as my primary interface.  Having an easy way
>> to convert the notmuch attachments to text other than saving to a file
>> and processing them would greatly speed up my workflow.
>>
>> Is there something in existance already to do this sort of thing?
>>
>> I have a little rudimentary lisp skill so can hack something up if
>> someone can give me some pointers on a direction to head in.
>>
>> Any advice appreciated.
>>
>> Kind regards
>>
>> Bart
>>
>> Kind regards
>> Bart
>> -- 
>>
>> Bart Bunting - URSYS
>> PH: 02 87452811
>> Mbl: 0409560005
>> _______________________________________________
>> notmuch mailing list
>> notmuch at notmuchmail.org
>> https://notmuchmail.org/mailman/listinfo/notmuch
Bart
-- 

Bart Bunting - URSYS
PH: 02 87452811
Mbl: 0409560005


More information about the notmuch mailing list