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 내에 존재하는 ... 태그를 찾아서 삭제
# 태그는 base64 디코딩을 수행할 때 오류가 발생하므로 삭제합니다.
# 태그와 그 내부 내용을 삭제합니다.
encoded_data = re.sub(b'', b'', binary_data)
# encoded_data = re.sub(b'', b'', encoded_data)
# print(encoded_data)
encoded_data = encoded_data.replace(b'', b'')
encoded_data = encoded_data.replace(b'\r\n', b'')
# print(encoded_data+b'==')
# base64 디코딩을 수행합니다.
decoded_data = base64.b64decode(encoded_data+b'==')
print(decoded_data)
# 디코딩된 데이터 내용 중 xml 형식만 추출할 때 , 사이의 데이터만 추출.
start = decoded_data.find(b'')
print(end)
xml_data = decoded_data[start:end+len(b'')]
# 디코딩된 데이터를 파일로 저장합니다.
with open('ext_BinData.xml', 'wb') as file:
file.write(xml_data)
print("Decoding complete. Decoded data saved to 'decoded_chartBinData'.")