Skip to content

Instantly share code, notes, and snippets.

@esehara
Created October 27, 2011 14:30
Show Gist options
  • Select an option

  • Save esehara/1319694 to your computer and use it in GitHub Desktop.

Select an option

Save esehara/1319694 to your computer and use it in GitHub Desktop.
SHPAML -> HTML -> PDF -> NetPrint
# -*- coding:utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys
import os
import shpaml
import BeautifulSoup
import mechanize
argvs = sys.argv
user_id = "FIXME"
user_pass = "FIXME"
user_mail = "FIXME@gmail.com"
def uploadNetPrint(filename):
br = mechanize.Browser()
response = br.open("https://www.printing.ne.jp/cgi-bin/mn.cgi?i=" + user_id + "&p=" + user_pass)
print "Login Success..."
page = BeautifulSoup.BeautifulSoup(response.read())
session = page.find("input",attrs={"name":"s","type":"hidden"}).attrs[2][1].encode("utf-8")
print "Session Get..."
br.open("https://www.printing.ne.jp/cgi-bin/mn.cgi?s=" + session + "&c=0&m=1")
print "Set Form"
br.select_form("uploadform")
br.form.add_file(open(os.path.abspath(".") + "/" + filename + ".pdf"),"application/pdf",os.path.abspath(".") + "/" + filename + ".pdf")
br["papersize"] = ["2"]
br["color"] = ["0"]
br["number"] = ["0"]
br["secretcodesw"] = ["0"]
br["mailsw"] = ["1"]
br["mailaddr"] = user_mail
br.submit()
print "Cool..."
def makehtml(filename):
filesystem = open(filename + ".html","w")
filesystem.write(shpaml.convert_text((open(filename + ".haml","r").read())))
filesystem.close()
def makepdf(filename):
def convertFile():
web.print_(printer)
print "done"
QApplication.exit()
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl(os.path.abspath(".") + "/" + filename + ".html"))
printer = QPrinter( QPrinter.HighResolution )
printer.setPageSize( QPrinter.A3 )
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filename + ".pdf")
QObject.connect(web,SIGNAL("loadFinished(bool)"),convertFile)
app.exec_()
def main():
if len(argvs) != 2:
print "Usage: python myself.py [file *.haml]"
exit()
if argvs[1].split(".")[1] != "haml":
print "Oops !! It uses Haml(SHPAML) Template Engine... Sorry..."
filename = argvs[1].split(".")[0]
print "Make Html File..."
makehtml(filename)
print "Make PDF File..."
makepdf(filename)
print "OK,Go NetPrint"
uploadNetPrint(filename)
print "Good :) Your Haml is Published!!"
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment