Created
October 13, 2017 22:40
-
-
Save jonathanmach/1036f5b68f2397696b41556e4bb7751a to your computer and use it in GitHub Desktop.
Encoding file to Base64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import base64 | |
image = open('demo.pdf', 'rb') # open binary file in read mode | |
image_read = image.read() | |
image_64_encode = base64.b64encode(image_read) | |
# Decode test | |
image_64_decode = base64.decodestring(image_64_encode) | |
image_result = open('demo_decoded.pdf', 'wb') # create a writable image and write the decoding result | |
image_result.write(image_64_decode) | |
# Creates an XML containing the Base64 encoding | |
root = ET.Element("Metadata", name = "AnexoImagemB64") | |
doc = ET.SubElement(root, "data") | |
doc.text = image_64_encode | |
ET.SubElement(doc, "dimension").text = 1 | |
#ET.SubElement(doc, "field1", name="image_64_decode").text = image_64_encode | |
tree = ET.ElementTree(root) | |
tree.write("filename.xml") | |
# Reading XML | |
tree = ET.parse('C:\Users\Jonathan\OneDrive\Workspace\PDF2Base64\pseries.xml') | |
root = tree.getroot() | |
# STEP 3: EXPORTAR XML PARA BASE DE DADOS | |
tree = ET.ElementTree(root) | |
f = BytesIO() # used to write the XML document to a fake file | |
tree.write(f, encoding='utf-8') # version="1.0" encoding="utf-8" standalone="yes" | |
xml_file = f.getvalue() | |
# Insert entry in the DOF table | |
logging.info('Exportando story %s' % item_fields.item_name) | |
update_dof_database(xml_file, story, d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment