# How can I execute a python script on a given region of my buffer?

I have a .tex source and a python script called comments.py. This script reads my .tex source as input, does some operations on it and write a .com file as output. Then I have to reneame .com as .tex and go on with my work.

I want to automate these procedure and so I have written a script in elisp to do this.

My problem

I want that comments.py acts on a given region of my .tex source and not on all buffer. So, how can I contain its actions from (point-min) to a point defined as \begin{thebibliography}?

Here there is my code:

;; clean comments on .tex sources

;; define .tex file name with extension
(setq source-tex-ext (format "%s" (buffer-name)))

(interactive)
(insert
(shell-command-to-string
(concat
(format "python %s" python-script-path) (format " %s" buffer-file-name)))))

(save-buffer)

;; close buffer
(kill-buffer (current-buffer))

;;
;;
;; Now I call a bash script in order to rename .tex as .bkp and then .com as .tex
;; because I am not able to do this in elisp directly
;;
;;

;; call rename-com-to-tex.sh
(defun call-bash-script ()
(interactive)
(setq bash-script-path "~/myscripts/rename-com-to-tex.sh")
(call-process "/bin/bash" nil t nil bash-script-path))

(call-bash-script)

;; open .tex (this is .com renamed as .tex)
(find-file (format "%s" source-tex-ext))

(save-buffer)

;; do other things
...


Hope my question is clear.

Hint

This is an attempt with shell-command-on-region as suggested (I have tried to construct the following code looking for examples):

(setq m1 (make-marker))
(goto-char (point-min))
(set-marker m1 (point-min))

(setq m2 (make-marker))
(set-marker m2 (search-forward "\\begin{thebibliography}"))

(interactive "r")
(shell-command-on-region
start-position end-position (concat
(format "python %s" python-script-path) (format " %s" (buffer-file-name))) (buffer-file-name) nil))


In this way the code creates a .com file as output, but restrictions are ignored. In the new .com file, comments.py has acted until the end and not until \begin{thebibliography}.
• What about shell-command-on-region? – wvxvw May 31 '18 at 15:15
• Can comments.py read from standard input instead of taking a filename? – npostavs Jun 1 '18 at 0:09
• @npostavs In the source of comments.py appears open(...), then I think that it must have a file as input, but I am not a python programmer, so I do not asnwer about this. I can not publish comments.py for confidentiality obligations. – Onner Irotsab Jun 1 '18 at 7:43
• @OnnerIrotsab you can try using shell redirection python comments.py < some-file and see if it works. – npostavs Jun 1 '18 at 8:54