Created
April 21, 2014 04:27
-
-
Save tphalp/11132219 to your computer and use it in GitHub Desktop.
PFSense voucher CSV to HTML with pagebreaks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
## Modified from http://pastebin.com/6H51NcU3 to add pagebreaks to the html markup. | |
def csv_to_htm(filename, minutes): | |
outfilename = filename.split('.')[0] + '.html' | |
table = '<table border="0" cellspacing="0" align="center" style="border-collapse:collapse;' | |
tablestart = table + '">\n' | |
tablestartbreak = table + 'page-break-before:always;">\n' | |
try: | |
csv_file = open(filename,"r") | |
except IOError as e: | |
if e.errno == 2: | |
print 'Unable to open file: ' + filename | |
sys.exit() | |
vouchers = [] | |
for line in csv_file.readlines(): | |
if line.find('#') != 0: | |
line = line.replace('"','') | |
line = line.replace(' ','') | |
line = line.replace('\n','') | |
vouchers.append(line) | |
try: | |
html_file = open(outfilename,"w") | |
except Exception as e: | |
print e | |
html_file.write('<!DOCTYPE html><html><head></head><body>\n') | |
html_file.write(tablestart) | |
colcount = 0 | |
rowcount = 0 | |
for code in vouchers: | |
if colcount > 3: | |
html_file.write('</tr>\n') | |
colcount = 0 | |
rowcount += 1 | |
if colcount == 0: | |
if rowcount == 4: | |
html_file.write('</table>') | |
html_file.write(tablestartbreak) | |
rowcount = 0 | |
html_file.write('<tr style="font-family:sans-serif">\n') | |
html_file.write('<td style="border:1px solid #000;padding:30px;"><center>YOURNETWORK<br>') | |
html_file.write('Internet Voucher<br><br>') | |
html_file.write('One Time Use<br><br>') | |
html_file.write('Valid For<br>' + str(minutes) + ' Minutes<br><br>') | |
html_file.write('<b style="font-family:monospace; font-size:larger;">' + code + '</b></center></td>') | |
colcount += 1 | |
html_file.write('</table></body></html>') | |
files = os.listdir('.') | |
csv_files = [] | |
csv_durations = [] | |
for item in files: | |
if item.endswith('.csv'): | |
csv_files.append(item) | |
for csv_file in csv_files: | |
valid_input = False | |
while valid_input != True: | |
minutes = raw_input('Enter the amount of time for vouchers in file ' + csv_file + ': ') | |
minutes = minutes.strip() | |
if minutes.isdigit(): | |
csv_durations.append(minutes) | |
valid_input = True | |
for filename, minutes in zip(csv_files, csv_durations): | |
print 'Generating vouchers: ' + filename, minutes | |
csv_to_htm(filename, minutes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment