Skip to content

Commit

Permalink
Added PDF Cheatsheet print function
Browse files Browse the repository at this point in the history
Current version is double-sided print
  • Loading branch information
Brian Lin committed Oct 12, 2019
1 parent 00ec499 commit dd0d844
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
6 changes: 0 additions & 6 deletions getKeyWords.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,3 @@ def sample_analyze_entities(text_content):
response = client.analyze_entities(document, encoding_type=encoding_type)
return [entity.name for entity in response.entities]


if __name__ == '__main__':
text = input()
keywords = sample_analyze_entities(text)
questions = get_blank_questions(text, keywords, n_questions = 10, n_blanks = int(input()))
print(questions)
106 changes: 106 additions & 0 deletions getPDF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from reportlab.pdfgen import canvas
from hashlib import sha256
from getKeyWords import sample_analyze_entities
from fill_blanks import get_blank_questions
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch, mm
from reportlab.platypus import (Flowable, Paragraph,SimpleDocTemplate, Spacer)


def getHash(text):
m = sha256()
m.update(str.encode(text))
return m.hexdigest()

def drawText(x,y, c, text):
textobject = c.beginText()
textobject.setTextOrigin(x, y)
textobject.setFont('Times-Roman', 14)
textobject.textLine(text)
c.drawText(textobject)

class BoxText(Flowable):

#----------------------------------------------------------------------
def __init__(self, x=0*inch, y=0*inch, width=3.68*inch, height=2.45*inch, text1="", text2=""):
Flowable.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.text1 = text1
self.text2 = text2
self.styles = getSampleStyleSheet()

#----------------------------------------------------------------------
def coord(self, x, y, unit=1):
"""
http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab
Helper class to help position flowables in Canvas objects
"""
x, y = x * unit, self.height - y * unit
return x, y

#----------------------------------------------------------------------
def draw(self):
"""
Draw the shape, text, etc
"""
self.canv.rect(self.x, self.y, self.width, self.height)
self.canv.rect(self.x + self.width, self.y, self.width, self.height)
p1 = Paragraph(self.text1, style=self.styles["Normal"])
p1.wrapOn(self.canv, self.width - 10, self.height)
p1.drawOn(self.canv, *self.coord(self.x+0.1, self.y + 2.40 , inch))
p2 = Paragraph(self.text2, style=self.styles["Normal"])
p2.wrapOn(self.x + self.width, self.width - 10, self.height)
p2.drawOn(self.canv, *self.coord(self.x + 3.68 +0.1, self.y + 2.40 , inch))

def getString(answers):
string = ""
for answer in answers[1]:
string += "Index = [" + str(answer[0]) + "] Solution = [" + answer[1] + "]; "
return string

def getPDF():
text = input()
keywords = sample_analyze_entities(text)
questionAnswers = get_blank_questions(text, keywords, n_questions = 10, n_blanks = int(input()))
doc = SimpleDocTemplate("card_" + getHash(text) + ".pdf",pagesize=letter,rightMargin=0.5 * inch, leftMargin=0.5 * inch,
topMargin=0.5 * inch, bottomMargin=0.5 * inch)

story = []
i = 0
while i < len(questionAnswers):
# draw questions
j = 0
while j < 8 and i + j < len(questionAnswers):
if j + 1 < len(questionAnswers):
box = BoxText(text1 = questionAnswers[j][0], text2= questionAnswers[j + 1][0])
story.append(box)
j += 2
else:
if j + 1 == len(questionAnswers):
box = BoxText(text1 = questionAnswers[j][0])
story.append(box)
j += 2
while j < 8:
# Add boxes until fill up page.
story.append(BoxText())
j += 2
# drawAnswers
j = 0
while j < 8 and i + j < len(questionAnswers):
if j + 1 < len(questionAnswers):
box = BoxText(text1 = getString(questionAnswers[j + 1]) , text2= getString(questionAnswers[j]))

else:
box = BoxText(text2= getString(questionAnswers[j]))
story.append(box)
j += 2
i += 8

doc.build(story)

if __name__ == "__main__":
getPDF()

0 comments on commit dd0d844

Please sign in to comment.