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)
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()
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.
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"