Files
diw/base64Decoder.py

43 lines
1.6 KiB
Python
Raw Normal View History

import base64
import re
from lxml import etree as ET
xml_path = r"C:\Users\dra\project\HWP-Scoring\output\워드(한글)-009866-성유나.hml"
tree = ET.parse(xml_path)
root = tree.getroot()
# xpath로 바이너리 부분추출
binary_data = root.xpath('//BINDATA[@Id=//BINITEM[@Format="OLE"]/@BinData]/text()')
binary_data = binary_data[0].encode('utf-8')
# 파일을 읽어들입니다.
# with open('./chartBinData2', 'rb') as file:
# encoded_data = file.read()
# encoded_data 내에 존재하는 <BINDATA ...> ... </BINDATA> 태그를 찾아서 삭제
# <BINDATA ...> 태그는 base64 디코딩을 수행할 때 오류가 발생하므로 삭제합니다.
# <BINDATA ...> 태그와 그 내부 내용을 삭제합니다.
encoded_data = re.sub(b'<BINDATA.*?>', b'', binary_data)
# encoded_data = re.sub(b'<BINDATA.*?>', b'', encoded_data)
2025-01-15 16:09:00 +09:00
# print(encoded_data)
encoded_data = encoded_data.replace(b'</BINDATA>', b'')
2025-01-15 16:09:00 +09:00
encoded_data = encoded_data.replace(b'\r\n', b'')
# print(encoded_data+b'==')
# base64 디코딩을 수행합니다.
2025-01-15 16:09:00 +09:00
decoded_data = base64.b64decode(encoded_data+b'==')
print(decoded_data)
# 디코딩된 데이터 내용 중 xml 형식만 추출할 때 <c:chartSpace>, </c:chartSpace> 사이의 데이터만 추출.
start = decoded_data.find(b'<?xml')
2025-01-15 16:09:00 +09:00
print(start)
end = decoded_data.find(b'</c:chartSpace>')
2025-01-15 16:09:00 +09:00
print(end)
xml_data = decoded_data[start:end+len(b'</c:chartSpace>')]
# 디코딩된 데이터를 파일로 저장합니다.
with open('ext_BinData.xml', 'wb') as file:
file.write(xml_data)
print("Decoding complete. Decoded data saved to 'decoded_chartBinData'.")