How to base64 encode a PDF file in Python

How should I base64 encode a PDF file for transport over XML-RPC in Python?


Asked by: Richard895 | Posted: 05-10-2021






Answer 1

If you don't want to use the xmlrpclib's Binary class, you can just use the .encode() method of strings:

a = open("pdf_reference.pdf", "rb").read().encode("base64")

Answered by: Marcus970 | Posted: 06-11-2021



Answer 2

Actually, after some more digging, it looks like the xmlrpclib module may have the piece I need with it's Binary helper class:

binary_obj = xmlrpclib.Binary( open('foo.pdf').read() )

Here's an example from the Trac XML-RPC documentation


import xmlrpclib 
server = xmlrpclib.ServerProxy("http://athomas:password@localhost:8080/trunk/login/xmlrpc") 
server.wiki.putAttachment('WikiStart/t.py', xmlrpclib.Binary(open('t.py').read())) 

Answered by: Agata223 | Posted: 06-11-2021



Answer 3

You can do it with the base64 library, legacy interface.

Answered by: Robert629 | Posted: 06-11-2021



Answer 4

Looks like you might be able to use the binascii module

binascii.b2a_base64(data)

Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char. The length of data should be at most 57 to adhere to the base64 standard.

Answered by: Michelle386 | Posted: 06-11-2021



Similar questions

email - Test for base64 encoding with python

I'm having some trouble with this bit of python. def get_msg( message ): if not message.is_multipart(): if "base64" in str(message.get_payload()): return base64.decodestring(str(message.get_payload())) return message.get_payload() return '\n\n'.join( [base64.decodestring(str(m)) for m in message.get_payload()] ) Lines 3 and 4 to be exact. If th...


base64 encoding of file in Python

I want to use the function base64.encode() to directly encode the contents of a file in Python. The documentation states: base64.encode(input, output) Encode the contents of the binary input file and write the resulting base64 encoded data to the output file. ...


Base64 encoding issue in Python

I need to save a params file in python and this params file contains some parameters that I won't leave on plain text, so I codify the entire file to base64 (I know that this isn't the most secure encoding of the world but it works for the kind of data that I need to use). With the encoding, everything works well. I encode the content of my file (a simply txt with a proper extension) and save the file. The problem ...


Encoding email as base64 in Python 3

I'm trying to create and send an email using gmail, based on the instructionshere to encode as base64. I create the message in this way: import smtplib from email.mime.text import MIMEText import base64 to='name@gmail.com' sender = 'me@email.com' subject = 'Menu' message_text = 'Spam' message = MIMEText(me...


python - How to disable HTML encoding when using Context in django

In my django application I am using a template to construct email body, one of the parameters is url, note there are two parametes separated by ampersand in the url. t = loader.get_template("sometemplate") c = Context({ 'foo': 'bar', 'url': 'http://127.0.0.1/test?a=1&b=2', }) print t.render(c) After rendering it produces: http://127.0.0.1/test?a=1&b=2...


python - Trouble with encoding in emails

I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email) Then a PHP script runs through the files and displays them. I am having an issue with ISO-8859-1 (Latin-1) encoded email Here's an example of the text i get: =?iso-8859-1?Q?G=EDsli_Karlsson?= and Sj=E1um hva=F0 =F3li er kl=E1r J The way i pull emails is this code.


Python mailbox encoding errors

First, let me say that I'm a complete beginner at Python. I've never learned the language, I just thought "how hard can it be" when Google turned up nothing but Python snippets to solve my problem. :) I have a bunch of mailboxes in Maildir format (a backup from the mail server on my old web host), and I need to extract the emails from these. So far, the simplest way I've found has been to convert them to the mbox ...


python - How to handle unicode of an unknown encoding in Django?

I want to save some text to the database using the Django ORM wrappers. The problem is, this text is generated by scraping external websites and many times it seems they are listed with the wrong encoding. I would like to store the raw bytes so I can improve my encoding detection as time goes on without redoing the scrapes. But Django seems to want everything to be stored as unicode. Can I get around that somehow?


What encoding do I need to display a GBP sign (pound sign) using python on cygwin in Windows XP?

I have a python (2.5.4) script which I run in cygwin (in a DOS box on Windows XP). I want to include a pound sign (£) in the output. If I do so, I get this error: SyntaxError: Non-ASCII character '\xa3' in file dbscan.py on line 253, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details OK. So I looked at that PEP, and now tried adding this to the beginning of ...


unicode - Python IRC bot and encoding issue

Currently I have a simple IRC bot written in python. Since I migrated it to python 3.0 which differentiates between bytes and unicode strings I started having encoding issues. Specifically, with others not sending UTF-8. Now, I could just tell everyone to send UTF-8 (which they should regardless) but an even better solution would be try to get python to default to some other encoding or such. So far...


encoding - Best way for Parsing ANSI and UTF-16LE files using Python 2/3?

I have a collection of files encoded in ANSI or UTF-16LE. I would like python to open the files using the correct encoding. The problem is that the ANSI files do not raise any sort of exception when encoded using UTF-16le and vice versa. Is there a straightforward way to open up the files using the correct file encoding?


python - Working with a QString encoding

Is there a Python library which can detect (and perhaps decode) encoding of the string? I found chardet but it gives me an error, using: chardet.detect(self.ui.TextFrom.toPlainText()) got: = chardet.detect(self.ui.TextFrom.toPlainText()) File .... u.feed(aBuf) File .... if self._highBitDetector.search(aBuf): T...


unicode - Adding encoding alias to python

Is there a way that I can add alias to python for encoding. There are sites on the web that are using the encoding 'windows-1251' but have their charset set to win-1251, so I would like to have win-1251 be an alias to windows-1251


Python JSON encoding

I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. Currently I am declaring a list, looping through and another list, and appending one list within another: import ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top