TeXcircuit.py

I have just automated the tedious process of conversion of Xcircuit generated postscript (.ps) file and LaTeX (.tex) into a combined PDF file. It also helps to generate transparent Portable Network Graphics (.png) file. Enjoy this automation tool !

Note : I have tested this program in Ubuntu 18.04.02 LTS with packages like “texlive-full” installed. I have checked this in Windows too, it works great with “Texlive full” installed. However I believe that if all necessary packages are installed in Windows, it would work flawlessly as python is cross-platform.

Copy the plain text from below as save it as texcircuit.py and make it executable.

Disclaimer : I am learning python 🙂

#!/usr/bin/env python
import sys
import os
print("TexCircuit Version 1, 23/7/2019 - Copyright (c) by Subha Sarkar, all other programs hold to their respective Copyright owner")
#Just take care that whatever this program read is properly closed by other programs
print("\n")
print("################ TexCircuit Comment ################")
print("Please take care if any of the file you entered in the argument is not open somewhere else or not properly closed !")
print("################ TexCircuit Comment ################")
print("\n")
bashcommand = "epstopdf " + sys.argv[1] +".ps"
os.system(bashcommand)
bashcommand = "pdfcrop " + sys.argv[1] + ".pdf"
os.system(bashcommand)
#Conversion of .ps file to .pdf file only
newfilename = sys.argv[1] + "-crop.pdf"
filename = sys.argv[1] + ".pdf"
os.remove(filename)
os.rename(newfilename,filename)
#Till here only pdf was created to be read in Tex file generated in Xcircuit
#########################################
#Now editing the tex file generated by Xcircuit
#second argument has to scale settings of Xcircuit
if len(sys.argv) >= 3:
scale = sys.argv[2]
else:
print("No scale parameter provided so searching for default scale = 1")
scale = '1'
#First Xircuit reading the tex file for the first time.
texfile = open(sys.argv[1]+".tex",'r')
#Duplicate file with replaced content
tempfile = open(sys.argv[1]+"_1.tex","w+")
#String to be find
to_find_string = "\scalebox{"+scale+"}"
#String to be find
to_find_string1 = "\includegraphics"
for myline in texfile:
if myline.find(to_find_string) >= 0:
tempfile.write("\\resizebox{\\textwidth}{!}{\n")
elif myline.find(to_find_string1) >= 0:
if sys.platform.startswith('linux'): #for linux
tempfile.write("\\includegraphics[scale=" + scale + "]{" + os.getcwd() + "/" + sys.argv[1] + "}\\\\")
print("System : Linux")
elif sys.platform.startswith('win32'): #for windows
tempfile.write("\\includegraphics[scale=" + scale + "]{" + sys.argv[1] + "}\\\\")
print("System : Windows")
elif sys.platform.startswith('darwin'): #for MacOS
tempfile.write("\\includegraphics[scale=" + scale + "]{" + os.getcwd() + "/" + sys.argv[1] + "}\\\\")
print("System : MacOS")
else:
tempfile.write(myline)
tempfile.close()
texfile.close()
texcontent = "\\documentclass{article} \n\\usepackage{graphicx} \n\\usepackage{xcolor} \n\\pagestyle{empty} \n\\begin{document} \n    \\include{" + sys.argv[1] +"_1" + "} \n\\end{document}"
#This is the final file which has to compiled by the latex
finalfilename = sys.argv[1] + "_2"
finalfile = open(finalfilename + ".tex","w+")
finalfile.write(texcontent)
#Following code showing that the file "finalfile" is closed before executing os command
finalfile.close()
print("\n")
print("################ TexCircuit Comment ################")
print("Please make sure that pdflatex and required packages are already installed in this system")
print("If not installed then you can install it by \"sudo apt-get install texlive-full\" (3.5 GB)")
print("################ TexCircuit Comment ################")
print("\n")
#Compiling the final latex file
bashcommand = "pdflatex " + finalfilename + ".tex"
os.system(bashcommand)
#We can search if pdfcrop package is there or not #thats an addon
#After executing this command a new file with extenstion -crop.pdf is generated
bashcommand = "pdfcrop " + finalfilename + ".pdf"
os.system(bashcommand)
#So it is required to delete the first file after pdfcrop is executed and then rename the final file with original file
#If ever pdfcrop program changes naming scheme then please edit this line
os.remove(finalfilename + ".pdf")
os.remove(filename)
#rename command
os.rename(finalfilename + "-crop.pdf",filename)
print("\n")
print("################ TexCircuit Comment ################")
print("Make sure pdftocairo is already installed (if you have texlive-full installed then the required package is already installed), if png conversion is not working.")
print("################ TexCircuit Comment ################")
print("\n")
print("Wait for few moments for PNG conversion ...")
#converting into PNG also
bashcommand = "pdftocairo -png -r 1000 -transp " + filename + " " + sys.argv[1]
os.system(bashcommand)
#remove again
os.remove(finalfilename+".tex")
os.remove(finalfilename+".log")
os.remove(sys.argv[1] + "_1.tex")
os.remove(finalfilename+".aux")
os.remove(sys.argv[1] + "_1.aux")
print("All done !")
print("PDF and PNG file written in following directory : " + os.getcwd())
#Just take care that whatever this program read is properly closed by other programs
#Now I have to remove everything except pdf and original Xcircuit and Tex file (accept permission from terminal)