简体   繁体   English

如何在Python中对PDF文件进行base64编码

[英]How to base64 encode a PDF file in Python

我应该如何对XML文件进行base64编码以便通过Python中的XML-RPC进行传输?

如果您不想使用xmlrpclib的Binary类,则可以使用字符串的.encode()方法:

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

Actually, after some more digging, it looks like the xmlrpclib module may have the piece I need with it's Binary helper class: 实际上,经过更多的挖掘之后,看起来xmlrpclib模块可能具有我需要的Binary helper类:

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

Here's an example from the Trac XML-RPC documentation 这是Trac XML-RPC文档中的示例


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

您可以使用base64库 (旧版接口)来实现。

Looks like you might be able to use the binascii module 看起来您可能可以使用binascii模块

binascii.b2a_base64(data) binascii.b2a_base64(数据)

Convert binary data to a line of ASCII characters in base64 coding. 使用base64编码将二进制数据转换为一行ASCII字符。 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. 为了遵守base64标准,数据长度最多应为57。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM