Define a function to generate html pages in python - python

trying to create several web pages that contains tables from the read csv file and I tried to define a function to create html web pages rather than writing the same codes many times.
read my csv file:
infile = open("new.csv", "r")
data = []
for line in infile:
cols = line.split(",")
Oposition = cols[0]
Winner = cols[1]
Margin = cols[2]
Ground = cols[3]
Year = cols[4]
pair = (Oposition, Winner, Margin, Ground, Year)
data.append(pair)
infile.close()
so far my codes are:
page = """<!DOCTYPE html>
<html>
<head>
<title>abc</title>
<style>
h1 {
text-align: center;
}
body {
background-image: url("2014.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
</style>
</head>
<body>
<h1>{{heading}}</h1>
{{paragraph}}
<p>Back to main page</p>
<table border="1"><tr><th>Oposition</th><th>Winner</th><th>Margin</th><th>Ground</th><th>Year</th></tr>
"""
out1=page.format(heading = "2012 Stats", paragraph = "<p>aaaaaaa</p>")
out2 = page.format(heading = "2013 Stats", paragraph = "<p>bbbbb</P>")
out3 = page.format(heading = "2014 Stats", paragraph = "<p>cccc</P>")
out4 = page.format(heading = "2015 Stats", paragraph = "<p>ddddCSK</p>")
def write_html_file(filename, body):
out = open(filename, "w")
out.write(body)
write_html_file("2012.html",out1)
write_html_file("2013.html",out2)
write_html_file("2014.html",out3)
write_html_file("2015.html",out4)
for r in data:
if ''.join(r[4].split()) == "2012":
Oposition = r[0]
Winner = r[1]
Margin = r[2]
Ground=r[3]
Year = r[4]
out1.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>")
elif ''.join(r[4].split()) == "2013":
Oposition = r[0]
Winner = r[1]
Margin = r[2]
Ground=r[3]
Year = r[4]
out2.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>")
elif ''.join(r[4].split()) == "2014":
Oposition = r[0]
Winner = r[1]
Margin = r[2]
Ground=r[3]
Year = r[4]
out3.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>")
elif ''.join(r[4].split()) == "2015":
Oposition = r[0]
Winner = r[1]
Margin = r[2]
Ground=r[3]
Year = r[4]
out4.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>")
def output(a):
a.write("</table> </body>\n")
a.write("</html>\n")
a.close()
output(out1)
output(out2)
output(out3)
output(out4)
im trying to make tables according to years 2012, 2013, 2014, 2015 and make html pages that contains each of them. just cant figure out.
Any help or other option? Much appreciated
I get an error message saying:
---> 25 page1 = page.format(heading = "2012 Stats", paragraph = "<p>aaaa</p>")
KeyError: '\n text-align'

Short Answer
Python's built-in string format syntax is a variable enclosed by single paramthesis. That's why '\n text-align' is regarded as a key whereas it was intended to be a css style.
Solution
You can go ahead to escape your css snippets but I would not recommend it, because the html text will not be readable and will be difficult to maintain.
Hence, please use a template engine to help. There are many template engines: Jinja2, Mako.. etc. Since I am more familiar with the first one, let me show you how to get page1 working::
from jinja2 import Environment
env = Environment()
page_template = env.from_string(page)
page1 = page_template(heading="2012 Stats", paragraph="<p>aaaaaaa</p>")
And you will need to install jinja2:
$ pip install jinja2
Alternative solution
You can use my library pyexcel and pyexcel-text to get a html table rendered for you. The sample code is:
import pyexcel as p
sheet = p.get_sheet(file_name='new.csv')
sheet.colnames = ['Oposition', 'Winner', 'Margin', 'Ground', 'Year']
sheet.name = "2012 Stats"
print(sheet.html)
To run above code, you need to install these two additional packages:
$ pip install pyexcel pyexcel-text

Related

Generating a low res image with clear text

I'm trying to create a table (width = 190px, height = 128px) that is filled with some text and save that as an image. I managed to do that both with matplotlib and by simply generating a HTML table however with both methods I run to the same problem the text gets "compressed" and is blurry. Blurry letters
I would like to generate a pic like this 32x16 pic hand drawn in paint. What would be the best way to achieve this. At the moment I am using python to generate the HTML table and then I am saving it using the html2image library. The table needs to be generated programmatically though. Furthermore I tried to sample only some of the pixels above a threshold but then the text is even worse. The image can be saved as .pgn, .jpg or .bmp Any advice is appreciated! The code below is how I am generating and saving the table ATM.
def printTable(data):
htmlTable = ""
htmlTable += '<table>'
htmlTable += '<tr>'
htmlTable += '<td>%s</td>'% "NAME"
htmlTable += '<td>%s</td>'% "LAST NAME"
htmlTable += '<td>%s</td>'% "PASSWORD"
htmlTable += '<td>%s</td>'% "USERNAME"
htmlTable+= '</tr>'
for element in data.itertuples():
htmlTable += '<tr>'
htmlTable += ('<td>%s</td>'% element.NAME)
htmlTable += ('<td>%s</td>'% element.LAST NAME)
htmlTable += ('<td>%s</td>'% element.PASSWORD)
htmlTable += ('<td>%s</td>'% element.USERNAME)
htmlTable += '</tr>'
htmlTable += '</table>'
return htmlTable
tableString = printTable(df1)
print(tableString)
from html2image import Html2Image
hti = Html2Image(size=(190, 128))
#hti = Html2Image()
html = tableString
css = "\
#font-face {\
font-family: myFirstFont;\
src: url(Dogica Pixel Regular.ttf);\
}\
table, th, td{ \
border:1px solid red; \
border-collapse: collapse; \
background-color: black; \
color:red \
}\
table{ \
widht: 190px;\
font-size: 8px;\
font-family:myFirstFont\
}\
"
hti.screenshot(html_str=html, css_str=css, save_as='htmlTable.png')
Original image
Scaled image
P.S. I really hope hand drawing every pixel on a canvas isn't the only solution xd.

How to convert QTableWidget data to PDF using PyQt5 Python

I am working on a python project where I have data stored in QTableWidget. I have to export this data into excel sheet and PDF. I have been able to export data to excel sheet using below code. But unable to understand how can I convert it to PDF.
filename, _ = QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
style = xlwt.XFStyle()
font = xlwt.Font()
font.bold = True
style.font = font
model = self.home_ui.reports_table.model()
for c in range(model.columnCount()):
text = model.headerData(c, QtCore.Qt.Horizontal)
first_col = sheet.col(c+1)
l = len(text)
first_col.width = (256 * l) + 1000
sheet.write(0, c + 1, text, style=style)
for r in range(model.rowCount()):
text = model.headerData(r, QtCore.Qt.Vertical)
sheet.write(r + 1, 0, text, style=style)
for c in range(model.columnCount()):
for r in range(model.rowCount()):
text = model.data(model.index(r, c))
sheet.write(r + 1, c + 1, text)
wbk.save(filename)
Above code is working fine and saving data to excel.
I have looked into other questions with same topic but all of them are in c++. I am looking for python equivalent.
Can anyone give me some good suggestion on how to convert data to PDF. Please help. Thanks
When you review an answer you should not only see the code but the solution itself, that is, the logic that is in the background. In this particular case the solution is to create an HTML that shows the table content, and use QTextDocument with QPrinter to print the HTML to PDF.
Considering the above, it is not necessary to do the translation since it is enough to implement it from scratch since the logic is clear.
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
app = QtWidgets.QApplication([])
w = QtWidgets.QTableWidget(10, 10)
for i in range(10):
for j in range(10):
it = QtWidgets.QTableWidgetItem("{}-{}".format(i, j))
w.setItem(i, j, it)
filename = "table.pdf"
model = w.model()
printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.PrinterResolution)
printer.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat)
printer.setPaperSize(QtPrintSupport.QPrinter.A4)
printer.setOrientation(QtPrintSupport.QPrinter.Landscape)
printer.setOutputFileName(filename)
doc = QtGui.QTextDocument()
html = """<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>"""
html += "<table><thead>"
html += "<tr>"
for c in range(model.columnCount()):
html += "<th>{}</th>".format(model.headerData(c, QtCore.Qt.Horizontal))
html += "</tr></thead>"
html += "<tbody>"
for r in range(model.rowCount()):
html += "<tr>"
for c in range(model.columnCount()):
html += "<td>{}</td>".format(model.index(r, c).data() or "")
html += "</tr>"
html += "</tbody></table>"
doc.setHtml(html)
doc.setPageSize(QtCore.QSizeF(printer.pageRect().size()))
doc.print_(printer)

How to set print() to a docx python

I have a code, that get selenium information and i need to print this information to the docx, but by template. Here i get information with help of print() (to set some part )
Stuyvesant High School
General Information
School Name:
Stuyvesant High School
Principal:
Mr. Eric Contreras
Principal’s E-mail:
ECONTRE#SCHOOLS.NYC.GOV
Type:
Regular school
Grade Span:
9-12
Address:
345 Chambers Street, New York, NY 10282
I printing this information in console, but i need print this information to the docx.
Here the part of code, where i print:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import openpyxl
import docx
from docx.shared import Pt
List = []
wb = openpyxl.load_workbook('D:\INSPR\Rating_100_schools\Top-100.xlsx')
sheet = wb['RI']
tuple(sheet['A1':'A100']) # Get all cells from A1 to A100.
for rowOfCellObjects in sheet['A1':'A100']:
for cellObj in rowOfCellObjects:
List.append(cellObj.value)
School_list_result = []
State = sheet.title
driver = webdriver.Chrome(executable_path='D:\chromedriver') #any path
def check_xpath(xpath):
try:
element = driver.find_element_by_xpath(xpath)
School_list_result.append(element.text)
except NoSuchElementException:
School_list_result.append("No data.")
def check_text(partial_link_text):
try:
element_text = driver.find_element_by_partial_link_text(partial_link_text)
School_list_result.append(element_text.get_attribute("href"))
except NoSuchElementException:
School_list_result.append("No data.")
def check_click(clicker):
try:
element_click = driver.find_element_by_partial_link_text(clicker)
element_click.click()
except NoSuchElementException:
print("No click.")
def get_url(url, _xpath, send_keys):
driver.get(url)
try:
_element = driver.find_element_by_xpath(_xpath)
_element.clear()
driver.implicitly_wait(10)
_element.send_keys(schools, send_keys)
_element.send_keys(u'\ue007')
driver.implicitly_wait(10)
except NoSuchElementException:
print("No data.")
for schools in List[98:100]:
#-----------------------------------------GREAT SCHOOLS-------------------------------------------
get_url("https://www.google.com/", '//*[#id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input'," " + State + " greatschools")
_clicker = driver.find_element_by_xpath('//*[#id="rso"]/div[1]/div/div[1]/a/h3').click()
check_xpath('//*[#id="hero"]/div/div[1]/h1') #School Name
check_xpath('/html/body/div[6]/div[8]/div/div[1]/div/div/div[2]/div[1]/div[2]/span[1]') #Principal
check_text('Principal email') #Principal’s E-mail
check_xpath('//*[#id="hero"]/div/div[2]/div[2]/div[3]/div[2]') #Grade Span
check_xpath('//*[#id="hero"]/div/div[2]/div[1]/div[1]/div[1]/div[1]/a/div/span[2]') #Address
check_xpath('/html/body/div[6]/div[8]/div/div[1]/div/div/div[2]/div[2]/span/a') #Phone
check_text('Website') #Website
check_xpath('//*[#id="hero"]/div/div[2]/div[1]/div[1]/div[1]/div[2]/a') #Associations/Communities
check_xpath('//*[#id="hero"]/div/div[2]/div[2]/div[1]/div/a/div[1]/div') #GreatSchools Rating
check_xpath('//*[#id="Students"]/div/div[2]/div[1]/div[2]') #Enrollment by Race/Ethnicity
#-----------------------------------------NCES-------------------------------------------
driver.implicitly_wait(10)
get_url("https://nces.ed.gov/search/index.asp?q=&btnG=Search#gsc.tab=0", '//*[#id="qt"]', " " + State)
check_click('Search for Public Schools - ')
driver.implicitly_wait(10)
check_xpath('/html/body/div[1]/div[3]/table/tbody/tr[4]/td/table/tbody/tr[7]/td[1]/font[2]') #School type
check_xpath('/html/body/div[1]/div[3]/table/tbody/tr[4]/td/table/tbody/tr[7]/td[3]/font') #Charter
check_xpath('/html/body/div[1]/div[3]/table/tbody/tr[12]/td/table/tbody/tr[3]/td/table/tbody/tr[2]/td/table/tbody')
#Enrollment by Gender
check_xpath('/html/body/div[1]/div[3]/table/tbody/tr[12]/td/table/tbody/tr[1]/td/table/tbody/tr[2]') #Enrollment by Grade
#-----------------------------------------USNEWS-------------------------------------------
driver.implicitly_wait(10)
url = "https://www.usnews.com/education/best-high-schools/new-york/rankings"
driver.get(url)
check_click(schools)
driver.implicitly_wait(10)
check_xpath('//*[#id="app"]/div/div/div/div[1]/div/div/div[2]/div[1]/div[2]/p[3]') #U.S.News Rankings
#-----------------------------------------PUBLIC REVIEW-------------------------------------------
driver.implicitly_wait(10)
get_url("https://www.google.com/", '//*[#id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input', " " + State + " publicschoolreview")
clicker = driver.find_element_by_partial_link_text('(2020)').click()
driver.implicitly_wait(10)
check_xpath('//*[#id="quick_stats"]/div/div[2]/ul/li[2]/strong') #Total # Students
check_xpath('//*[#id="total_teachers_data_row"]/td[2]') #Full-Time Teachers
check_xpath('//*[#id="quick_stats"]/div/div[2]/ul/li[3]/strong') #Student/Teacher Ratio
#-----------------------------------------PRINT INFOFMATION-------------------------------------------
print(" ---------------------------------------------------------------"+"\n",
" \033[1m", schools,"\033[0m"+"\n",
" ---------------------------------------------------------------"+"\n",
" \033[1mGeneral Information\033[0m "+"\n",
"\033[1mSchool Name:\n\033[0m",School_list_result[0]+"\n",
"\033[1mPrincipal:\n\033[0m",School_list_result[1]+"\n",
"\033[1mPrincipal’s E-mail:\n\033[0m",School_list_result[2]+"\n",
"\033[1mType:\n\033[0m",School_list_result[10]+"\n",
"\033[1mGrade Span:\n\033[0m",School_list_result[3]+"\n",
"\033[1mAddress:\n\033[0m",School_list_result[4]+"\n",
"\033[1mPhone:\n\033[0m",School_list_result[5]+"\n",
"\033[1mWebsite:\n\033[0m",School_list_result[6]+"\n",
"\033[1mAssociations/Communities:\n\033[0m",School_list_result[7]+"\n",
"\033[1mGreatSchools Summary Rating:\n\033[0m",School_list_result[8]+"\n",
"\033[1mU.S.News Rankings:\n\033[0m",School_list_result[14]+"\n",
" \033[1mSchool Details\033[0m"+"\n",
"\033[1mTotal # Students:\n\033[0m",School_list_result[15]+"\n",
"\033[1mFull-Time Teachers:\n\033[0m",School_list_result[16]+"\n",
"\033[1mStudent/Teacher Ratio:\n\033[0m",School_list_result[17]+"\n",
"\033[1mCharter:\n\033[0m",School_list_result[11]+"\n",
"\033[1mMagnet: \n\033[0m","No""\n",
" \033[1mEnrollment Data\033[0m"+"\n",
"\033[1mEnrollment by Race/Ethnicity: \n\033[0m",School_list_result[9]+"\n",
"\033[1mEnrollment by Gender: \n\033[0m",School_list_result[12]+"\n",
"\033[1mEnrollment by Grade: \n\033[0m",School_list_result[13]+"\n",
()
)
print()
School_list_result.clear()
What i need: print this result not into console by template, but into a docx by template.
And one more: if you know how to not using indexing (like: School_list_result[0]), please tell me.
I assume you are on a windows operating system just as I do, and know how to download python packages:
Install docx and python-docx modules (they are different, make sure you have installed both)
use the following code:
School_list_result = [
"Stuyvesant High School",
"Mr. Eric Contreras",
"ECONTRE#SCHOOLS.NYC.GOV",
"Regular school",
"9-12",
"345 Chambers Street, New York, NY 10282",
]
headers = [
"School Name: ",
"Principal: ",
"Principal's Email: ",
"Type: ",
"Grade Span: ",
"Address: ",
]
def print_into_one_doc():
import os
from docx import Document
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
# after you create a docx file, make sure you double click to open it, write some stuff, press ctrl + s, delete what you have written, press ctrl + s, close the document
# delete what you have written. Otherwise python-docx reports a Package Not Find Error.
p = input('hold shift key right click, copy and paste the file path of docx here: ')
if p[0] == '"' or p[0] == "'":
# validate path
p = p[1:-1]
p = os.path.abspath(p)
doc = Document(p)
h = doc.add_paragraph()
# make title align to center
h.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
r = h.add_run(School_list_result[0])
# set title color
r.font.color.rgb = RGBColor(54, 95, 145)
# set title size
r.font.size = Pt(36)
doc.add_paragraph('\n')
su = doc.add_paragraph()
ru = su.add_run('General Information')
ru.font.size = Pt(30)
for i, d in enumerate(headers):
sp = doc.add_paragraph()
rp = sp.add_run(headers[i])
rp.bold = True
rp.font.size = Pt(23)
sm = doc.add_paragraph()
rm = sm.add_run(School_list_result[i])
rm.font.size = Pt(22)
rm.italic = True
doc.add_page_break()
doc.save(p)
print_into_one_doc()
If you have a list, which contains School_list_result, iterate it through, here is an example:
List_of_school_list_result = [
[
"Stuyvesant High School",
"Mr. Eric Contreras",
"ECONTRE#SCHOOLS.NYC.GOV",
"Regular school",
"9-12",
"345 Chambers Street, New York, NY 10282",
],
[
"Great Lake College",
"Mr. Jason Madunic",
"MADUNIC#SCHOOLS.VIC.GOV",
"Public school",
"6-12",
"167A High Street, Melbourne, VIC 3228",
],
]
headers = [
"School Name: ",
"Principal: ",
"Principal's Email: ",
"Type: ",
"Grade Span: ",
"Address: ",
]
def print_all_into_one_doc():
import os
from docx import Document
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
# after you create a new docx file, double click to open it, write some stuff, press ctrl + s, delete what you have written, press ctrl + s, close the document
# Otherwise python-docx reports a Package Note Find Error.
p = input('hold shift key right click, copy and paste the file path of docx here: ')
if p[0] == '"' or p[0] == "'":
# validate path
p = p[1:-1]
p = os.path.abspath(p)
doc = Document(p)
# iterate List of all school
for j in List_of_school_list_result:
h = doc.add_paragraph()
# make title align to center
h.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
r = h.add_run(j[0])
# set title color: you can adjust any color of title here
r.font.color.rgb = RGBColor(54, 95, 145)
# set title size
r.font.size = Pt(36)
doc.add_paragraph('\n')
su = doc.add_paragraph()
ru = su.add_run('General Information')
ru.font.size = Pt(30)
for i, d in enumerate(headers):
sp = doc.add_paragraph()
rp = sp.add_run(headers[i])
rp.bold = True
rp.font.size = Pt(23)
sm = doc.add_paragraph()
rm = sm.add_run(j[i])
rm.font.size = Pt(22)
rm.italic = True
doc.add_page_break()
doc.save(p)
print_all_into_one_doc()
Let's make it simple, what you need to do is:
create a list named List_of_school_list_result, dump your data in, each of them should be one single record of a certain school.
in any location, create a new docx file, double click to open it, write some stuff, press ctrl + s, delete what you have written, press ctrl + s, close the document.
go to the directory where your docx file is, hold on shift, right click, copy as path.
make sure docx and python-docx are installed, run the code, when you are asked to input the path, paste it in from your clipboard. (Please make sure you use an absolute path, which is a full directory with root c, a relative path may not work).
PS: the reason that you have to open the docx file after create, is that Microsoft Word 2005+ docx file have 3 modes. first, if it's brand new after creation, it's in binary format. second, if we open it to edit, it generates a $cache.docx file as hidden into same level directory to ensure performance and secure data just in case of crash. third, if it's edited and saved, the format will be turned into XML, which is EDITABLE using python-docx module.
PS: the Result class below provides a clear way for creating List_of_school_list_result:
class Result:
def __init__(self, length):
self.l = length
self.res = []
self.col = []
def push(self, string):
self.col.append(string)
if(len(self.col) == self.l):
self.res.append(self.col)
self.col = []
def publish(self):
return self.res
r = Result(6) # pass in the length of the headers, then all you need, is to call `r.push()` over and over again. after that, assign it to `List_of_school_list_result`
r.push('school name 1')
r.push('principal name 1')
r.push('principal email 1')
r.push('school type 1')
r.push('grad span 1')
r.push('address 1')
r.push('school name 2')
r.push('principal name 2')
r.push('principal email 2')
r.push('school type 2')
r.push('grad span 2')
r.push('address 2')
List_of_school_list_result = r.publish()
Complete version of code:
headers = [
"School Name: ",
"Principal: ",
"Principal's Email: ",
"Type: ",
"Grade Span: ",
"Address: ",
]
class Result:
def __init__(self, length):
self.l = length
self.res = []
self.col = []
def push(self, string):
self.col.append(string)
if(len(self.col) == self.l):
self.res.append(self.col)
self.col = []
def publish(self):
return self.res
r = Result(len(headers))
# call r.push() over and over again, until all the string data is passed in.
''' for example
r.push('school name 1')
r.push('principal name 1')
r.push('principal email 1')
r.push('school type 1')
r.push('grad span 1')
r.push('address 1')
r.push('school name 2')
r.push('principal name 2')
r.push('principal email 2')
r.push('school type 2')
r.push('grad span 2')
r.push('address 2')
'''
List_of_school_list_result = r.publish()
def print_all_into_one_doc():
import os
from docx import Document
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
# after you create a new docx file, double click to open it, write some stuff, press ctrl + s, delete what you have written, press ctrl + s, close the document
# Otherwise python-docx reports a Package Note Find Error.
p = input('hold shift key right click, copy and paste the file path of docx here: ')
if p[0] == '"' or p[0] == "'":
# validate path
p = p[1:-1]
p = os.path.abspath(p)
doc = Document(p)
# iterate List of all school
for j in List_of_school_list_result:
h = doc.add_paragraph()
# make title align to center
h.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
r = h.add_run(j[0])
# set title color: you can adjust any color of title here
r.font.color.rgb = RGBColor(54, 95, 145)
# set title size
r.font.size = Pt(36)
doc.add_paragraph('\n')
su = doc.add_paragraph()
ru = su.add_run('General Information')
ru.font.size = Pt(30)
for i, d in enumerate(headers):
sp = doc.add_paragraph()
rp = sp.add_run(headers[i])
rp.bold = True
rp.font.size = Pt(23)
sm = doc.add_paragraph()
rm = sm.add_run(j[i])
rm.font.size = Pt(22)
rm.italic = True
doc.add_page_break()
doc.save(p)
print_all_into_one_doc()

Issue writing to file

I am having problems with writing to my HTML file.
I want to be able to make my code place what I have written in HTML, but my code will only do it when I do not have any While/If statements in this definition. If I do have Whiles/Ifs, my HTML file will simply just become blank. I have no idea why this happens, and I can't seem to find a workaround.
Is there any way to use Whiles/Ifs without making my code delete everything in my file, and instead make it write what I want to in there?
def invoice_creator_window():
global total_cost, total_cost_list, list_of_items, invoice_created
invoice_created = Toplevel()
invoice_created.focus_set()
invoice_created.resizable(width = True, height = False)
invoice_created.title("Invoice Created")
invoice_created.geometry("300x300")
invoice_created.geometry("+400+400")
invoice_created.configure(bg = "limegreen")
currentDisplay = 10
print(total_cost, total_cost_list, list_of_items)
done = Label(invoice_created, text = "Items have been purchased. Invoice has been created. Please check this program's file location.")
invoice_created = Button(invoice_created, text = "Done", bg = "white", command = close_window)
#
done.grid(row = 1, column = 1, padx = 7.5, pady = space_between)
invoice_created.grid(row = 2, column = 1, padx = 7.5, pady = space_between)
# This section is for the invoice creation with HTML.
html_formatting_start = """<!DOCTYPE html>
<html>
<head>
<title>Games R Us - Invoice</title>
</head>
<body>
<style>
body {background-color: #F7D358;}
h1 {color: #775A03;}
p {color: ; border: 1px solid #775A03; padding: 15px; width: 650px}
</style>
<center>
<h1>Games R Us - Invoice Document</h1>
"""
counter = 0
while len(list_of_items) > 0:
global html_formatting_mid
print(counter)
html_formatting_mid = ("""
<h3>
<p>
<img src="https://steamcdn-a.akamaihd.net/steam/apps/211420/header.jpg?t=1483694369"</img>
<br>""" + str(list_of_items[counter]) + """<br>
<i>$""" + str(total_cost_list[counter]) + """ AUD</i>
</p>
</h3>
""")
if counter >= len(list_of_items) - 1:
return
else:
counter += 1
html_formatting_end = """
<h2>In Total: $""" + str(total_cost) +""" AUD</h2>
<br>
<b>Information Grabbed from These Links: </b>
Steam's New Releases [LIVE] -
Steam's Top Sellers [LIVE] -
Umart's Headphones [PRE-DOWNLOADED] -
Umart's Microphones [PRE-DOWNLOADED]
</center>
</body>
</html>"""
invoice_creation = open(invoice_file, "w")
invoice_creation.write(html_formatting_start)
invoice_creation.write(html_formatting_mid)
invoice_creation.write(html_formatting_end)
invoice_creation.close()
#############################################################################
button_buy = Button(welcome_window, text = "Buy", fg = "white", bg = "goldenrod", font = gui_font_10,
command = invoice_creator_window)
Note: "total_cost_list" and "list_of_items" are all lists. "total_cost" is a value. Do not worry too much about the context of these, but I wanted to clarify in case they might be affecting anything.
Here is a fixed version of your code in as simple a container as possible.
from tkinter import *
import webbrowser
root = Tk ()
space_between, invoice_file = 5, "Invoice.html"
total_cost, total_cost_list, list_of_items = 10, [1, 9], ["Something", "Something 2"]
def close_window (): root.destroy ()
def invoice_creator_window():
global total_cost, total_cost_list, list_of_items, invoice_created
invoice_created = Toplevel()
invoice_created.focus_set()
invoice_created.resizable(width = True, height = False)
invoice_created.title("Invoice Created")
invoice_created.geometry("300x300")
invoice_created.geometry("+400+400")
invoice_created.configure(bg = "limegreen")
currentDisplay = 10
print(total_cost, total_cost_list, list_of_items)
done = Label(invoice_created, text = "Items have been purchased. Invoice has been created.")
invoice_created = Button(invoice_created, text = "Done", bg = "white", command = close_window)
#
done.grid(row = 1, column = 1, padx = 7.5, pady = space_between)
invoice_created.grid(row = 2, column = 1, padx = 7.5, pady = space_between)
# This section is for the invoice creation with HTML.
html_formatting_start = """<!DOCTYPE html>
<html>
<head>
<title>Games R Us - Invoice</title>
</head>
<body>
<style>
body {background-color: #F7D358;}
h1 {color: #775A03;}
p {color: ; border: 1px solid #775A03; padding: 15px; width: 650px}
</style>
<center>
<h1>Games R Us - Invoice Document</h1>
"""
counter = 0
html_formatting_mid = ""
while len(list_of_items) > 0:
print(counter)
html_formatting_mid += """
<h3>
<p>
<img src="https://steamcdn-a.akamaihd.net/steam/apps/211420/header.jpg?t=1483694369"</img>
<br>""" + str(list_of_items[counter]) + """<br>
<i>$""" + str(total_cost_list[counter]) + """ AUD</i>
</p>
</h3>
"""
if counter >= len(list_of_items) - 1:
break
else:
counter += 1
html_formatting_end = """
<h2>In Total: $""" + str(total_cost) +""" AUD</h2>
<br>
<b>Information Grabbed from These Links: </b>
Steam's New Releases [LIVE] -
Steam's Top Sellers [LIVE] -
Umart's Headphones [PRE-DOWNLOADED] -
Umart's Microphones [PRE-DOWNLOADED]
</center>
</body>
</html>"""
invoice_creation = open(invoice_file, "w")
invoice_creation.write (html_formatting_start + html_formatting_mid + html_formatting_end)
invoice_creation.close ()
webbrowser.open (invoice_file)
#############################################################################
button_buy = Button(root, text = "Buy", fg = "white", bg = "goldenrod",
command = invoice_creator_window).pack ()
root.mainloop ()
I found a solution.
def invoice_creator_window():
global total_cost, total_cost_list, list_of_items, invoice_created
invoice_created = Toplevel()
invoice_created.focus_set()
invoice_created.resizable(width = True, height = False)
invoice_created.title("Invoice Created")
invoice_created.geometry("300x300")
invoice_created.geometry("+400+400")
invoice_created.configure(bg = "limegreen")
currentDisplay = 10
print(total_cost, total_cost_list, list_of_items)
done = Label(invoice_created, text = "Items have been purchased. Invoice has been created. Please check this program's file location.")
invoice_created = Button(invoice_created, text = "Done", bg = "white", command = close_window)
#
done.grid(row = 1, column = 1, padx = 7.5, pady = space_between)
invoice_created.grid(row = 2, column = 1, padx = 7.5, pady = space_between)
# This section is for the invoice creation with HTML.
html_formatting_start = """<!DOCTYPE html>
<html>
<head>
<title>Games R Us - Invoice</title>
</head>
<body>
<style>
body {background-color: #F7D358;}
h1 {color: #775A03;}
p {color: ; border: 1px solid #775A03; padding: 15px; width: 650px}
</style>
<center>
<h1>Games R Us - Invoice Document</h1>
"""
counter = 0
html_formatting_mid = ""
for items in list_of_items:
print(counter)
html_formatting_mid += ("""
<h3>
<p>
<img src="https://steamcdn-a.akamaihd.net/steam/apps/211420/header.jpg?t=1483694369"</img>
<br>""" + str(list_of_items[counter]) + """<br>
<i>$""" + str(total_cost_list[counter]) + """ AUD</i>
</p>
</h3>
""")
counter += 1
html_formatting_end = """
<h2>In Total: $""" + str(total_cost) +""" AUD</h2>
<br>
<b>Information Grabbed from These Links: </b>
Steam's New Releases [LIVE] -
Steam's Top Sellers [LIVE] -
Umart's Headphones [PRE-DOWNLOADED] -
Umart's Microphones [PRE-DOWNLOADED]
</center>
</body>
</html>"""
invoice_creation = open(invoice_file, "w")
invoice_creation.write(html_formatting_start)
invoice_creation.write(html_formatting_mid)
invoice_creation.write(html_formatting_end)
invoice_creation.close()
#############################################################################
button_buy = Button(welcome_window, text = "Buy", fg = "white", bg = "goldenrod", font = gui_font_10,
command = invoice_creator_window)
I went back to my older use of a for loop and tried using that. Except this time, I added html_formatting_mid += (""" to it, and then before the for loop, just added html_formatting_mid = "", like Minion Jim showed.
It works fully now.
A short answer would be, you could just write in append mode. open (filename, 'a') instead of open (filename, 'w'). This will add on to whatever is already in the file, so you may want to truncate it first.

Parsing Python textfile with tags

I am parsing a 300 page document with python and I need to find out the attribute values of the Response element after the ThisVal element. There are multiple points where the Response element is used for differentVals, so I need to find out what is in the Response elements attribute value after finding the ThisVal element.
If it helps, the tokens are unique to ThisVal, but are different in every document.
11:44:49 <ThisVal Token="5" />
11:44:49 <Response Token="5" Code="123123" elements="x.one,x.two,x.three,x.four,x.five,x.six,x.seven" />
Have you considered using pyparsing? I've found it to be very useful for this kind of thing. Below is my attempt at a solution to your problem.
import pyparsing as pp
document = """11:44:49 <ThisVal Token="5" />
11:44:49 <Response Token="5" Code="123123" elements="x.one,x.two,x.three,x.four,x.five,x.six,x.seven" />
"""
num = pp.Word(pp.nums)
colon = ":"
start = pp.Suppress("<")
end = pp.Suppress("/>")
eq = pp.Suppress("=")
tag_name = pp.Word(pp.alphas)("tag_name")
value = pp.QuotedString("\"")
timestamp = pp.Suppress(num + colon + num + colon + num)
other_attr = pp.Group(pp.Word(pp.alphas) + eq + value)
tag = start + tag_name + pp.ZeroOrMore(other_attr)("attr") + end
tag_line = timestamp + tag
thisval_found = False
for line in document.splitlines():
result = tag_line.parseString(line)
print("Tag: {}\nAttributes: {}\n".format(result.tag_name, result.attr))
if thisval_found and tag_name == "Response":
for a in result.attr:
if a[0] == "elements":
print("FOUND: {}".format(a[1]))
thisval_found = result.tag_name == "ThisVal"

Categories