<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BharathKumar (BK) Inbasekaran</title>
    <description>The latest articles on DEV Community by BharathKumar (BK) Inbasekaran (@bharathbk).</description>
    <link>https://dev.to/bharathbk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F119442%2Fb36451b6-e50c-42e9-90a5-8f0f7afcc6d5.jpeg</url>
      <title>DEV Community: BharathKumar (BK) Inbasekaran</title>
      <link>https://dev.to/bharathbk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bharathbk"/>
    <language>en</language>
    <item>
      <title>Create a document using python-docx and send as attachment through django</title>
      <dc:creator>BharathKumar (BK) Inbasekaran</dc:creator>
      <pubDate>Mon, 03 Dec 2018 21:12:32 +0000</pubDate>
      <link>https://dev.to/bharathbk/create-a-document-using-python-docx-and-send-as-attachment-through-django-11ko</link>
      <guid>https://dev.to/bharathbk/create-a-document-using-python-docx-and-send-as-attachment-through-django-11ko</guid>
      <description>

&lt;p&gt;I have created a document using docx and tried to send as an email attachment without saving the document on the server. Below is my code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document = document()
paragraph = document.add_paragraph("Test Content")
f = BytesIO()
document.save(f)
file_list = []
file_list.append(["Test.docx",f, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
email = EmailMessage(subject = 'Test', body = 'Hi', to = ['test@test.com'], attachments = file_list)
email.send()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I am getting the following error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TypeError: expected bytes-like object, not BytesIO&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;on the line &lt;code&gt;email.send()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I tried converting BytesIO to StringIO as mentioned &lt;a href="https://stackoverflow.com/questions/24566630/convert-io-bytesio-to-io-stringio-to-parse-html-page"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f = f.read()
f = StringIO(f.decode('UTF-8'))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;and then I get the error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TypeError: expected bytes-like object, not StringIO&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I looked at the solution from &lt;a href="https://stackoverflow.com/questions/46215083/create-word-document-and-then-attach-it-to-email-django"&gt;this&lt;/a&gt;, but didn't understand how the &lt;code&gt;document&lt;/code&gt; is sent as an attachment.&lt;/p&gt;

&lt;p&gt;Any help or pointers is appreciated.&lt;/p&gt;

&lt;p&gt;Thanks!&lt;/p&gt;


</description>
      <category>django</category>
      <category>python</category>
      <category>emailattachments</category>
      <category>help</category>
    </item>
  </channel>
</rss>
