0

Using Ubuntu 14.04 and Python 3.6

I currently have pyperclip installed, works great when creating shortcuts to open files in specific programs. I simply copy file to clipboard and open with specified app shortcut key.

I would like to do the same with ps2pdf except add .pdf to the destination file, in the same directory. No matter what I try, it keeps creating a pdf in my home directory. The correct syntax for ps2pdf is: ps2pdf Original_file.ps Destination_file.pdf

#!/usr/bin/env python3
import subprocess
import pyperclip

first = pyperclip.paste()

#text =`echo "$first" | cut -d'.' -f1`

text = first.substr(0, first.lastIndexOf(".")) + ".pdf";

subprocess.Popen(["ps2pdf", pyperclip.paste() " " text])

I've tried quite a few different variations, and variables, but I don't want to list them all here. Just shows 1 example of what else I've tried.

Any help would be appreciated.

Thank you

1 Answer 1

0
#!/usr/bin/env python3
import subprocess
import pyperclip
import os

os.chdir("/DirectoryPathOfFile")

subprocess.Popen(["ps2pdf", pyperclip.paste()])

This worked like a charm.

Now to elaborate even further of what I was trying to do. As we know there is an issue exporting to PDF in Ubuntu when filling out Adobe forms. You can only export to PS then convert via ps2pdf. Then for those of us who want a clean file we must use exiftool. Below is an example script to handle it all, including removing the _original file created by exiftool when working on the same file with same name in same dir.

I like the fact that you can watch it work in nautilus. I didn't trying using event.wait for this, wasn't completely sure how to implement, but also wanted to allow time for the hard drive to execute (being extra cautious). - Remember, this requires all dependencies of pyperclip to be installed. I linked this script to a shortcut key and can now Ctrl +c the .ps file in nautilus and (script shortcut key) so the script does the rest.

#!/usr/bin/env python3
import subprocess
import pyperclip
import os
import time

os.chdir("/DirectoryPathOfFile")

subprocess.Popen(["ps2pdf", pyperclip.paste()])

time.sleep(3)

new1 = os.path.splitext(pyperclip.paste())[0]+".pdf"

subprocess.Popen(["exiftool", "-all:all=", new1])

time.sleep(3)

new2 = os.path.splitext(pyperclip.paste())[0]+".pdf_original"

os.remove(new2)

#optionally remove original .ps file as well
#os.remove(pyperclip.paste())

I hope this helps everyone else as much as it did me!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .