Search

31 May, 2019

Send email with excel file as attachment

We want to send a mail which will have some content in it's body and an attachment of excel file. The excel file is not physically saved on anywhere. We have just created it, filled it and and now , we want to send it in an email.

So what we really have as an attachment is a workbook object.

Let's get started.


    from io import BytesIO
    output = BytesIO()
    attachment.save(output)
    msg = EmailMultiAlternatives(subject, "", from_email, to_email, cc=cc, bcc=bcc)
    msg.attach('my_file.xlsx', output.getvalue(), 'application/vnd.ms-excel')
    msg.attach_alternative(content, "text/html")
    try:
        msg.send()
        success = True
    except socket.gaierror:
        success = False
    return success



10 May, 2019

Nose test command line tricks

If you are using nose plugin to run your tests , there are some handy tricks that we can use while running tests.

Run test and generate a XML report:


nosetests --with-xunit test_folder --xunit-file=nosetests_test_results.xml

Django: Creating PDF , writing content and returning as a downloaded file

Hi guys,

It took me hours to make it finally work but working with PDFs is a pain. I want to share what worked for me.

The canvas should be thought of as a sheet of white paper with points on the sheet identified using Cartesian (X,Y) coordinates which by default have the (0,0) origin point at the lower left corner of the page.


from reportlab.pdfgen import canvas
import io

pdf_buffer = io.BytesIO()
p = canvas.Canvas(pdf_buffer)
p.drawString(0, 100, "Hello People")
p.showPage()
p.save()

response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="some_file.pdf"'
pdf_value = pdf_buffer.getvalue()
response.write(pdf_value)
return response

Above code , generates a pdf with the words "Hello People" written in it. 

15 April, 2019

PyCharms Handy short cuts

Hi friends. Today we are going to see some extremely useful (and must know) shortcuts for PyCharms .

Moving to last working point:


Ctrl+Shift+Backspace

04 April, 2019

How and when to use *args and **kwargs in Python

args and kwargs are special arguments (the words itself are just convention) which can be passed to a function.

13 March, 2019

Python Special Methods : __call__

Python has some methods that are special in a way . If  these methods are created inside a class, they can bring in some unique behaviors . We will talk about the method '__call__' today.

04 March, 2019