简体   繁体   中英

Encode/decode documents to base64 dynamically

How do I encode pdf and word files in a folder to base64 and decode them and save into the same folder? The pdf and word files are generated dynamically through a web service.

I would like to use python to do so.

I used this. But it gives the error Traceback (most recent call last):

File "sample.py", line 7, in base64.encode(open("hello.pdf"), open("hello1.b64", "w")) File "C:\\Python34\\lib\\base64.py", line 496, in encode s = input.read(MAXBINSIZE) File "C:\\Python34\\lib\\encodings\\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1340: character maps

base64.encode(open("hello.pdf"), open("hello1.b64", "w"))

The base64 module, which is included in the standard lib. The documentation is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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