N
N
Nonprogrammer2018-04-05 21:18:11
Python
Nonprogrammer, 2018-04-05 21:18:11

How to work with image bytes in JavaScript or how to rewrite from Python to JavaScript?

What do I want to do in JavaScript? I open a Word(doc) document with ActiveXObject('Word.Application'), find InlilneShape, use property shape.Range.EnhMetaFileBits to get the image bytes and try to base64 it to show in the browser.
Problem. According to documentation

The EnhMetaFileBits property returns an array of bytes , which can be used with the Microsoft Windows 32 Application Programming Interface from within the Microsoft Visual Basic or Microsoft C++ development environment.
I don't seem to be getting any bytes or an array of bytes from EnhMetaFileBits. How to get bytes and convert them to base64? Can you suggest a book about working with bytes in JavaScript.
function paragraphToHtml(paragraph) {
    for (let i = 1; i <= paragraph.Range.InlineShapes.Count; i++) {
        let shape = paragraph.Range.InlineShapes(i);
        let imageBits = shape.Range.EnhMetaFileBits;
        // Как работать с imageBits?
        console.log(typeof imageBits === 'unknown')  // true
        console.log(imageBits)  // ничего непоказывает
        // Что дальше делать?
    }
}

Working Python code to be rewritten to JScript
from PIL import Image
from io import BytesIO

def paragraph_to_html(para):
    img_temp = '<img align="Middle" src="data:image/png;base64,{0}" />'
    for shape in para.Range.InlineShapes:
        wmfim = Image.open(BytesIO(shape.Range.EnhMetaFileBits))
        bimage = BytesIO()
        wmfim.save(bimage, format='png')
        img_str = base64.b64encode(bimage.getvalue())
        shape.Range.Text = img_temp.format(img_str.decode('ascii'))
    return para.Range.Text.strip()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexPancho, 2018-09-20
@c_pro_lang

What a terrible overpower...
In python there is a library for working with dox that can search in a document
https://python-docx.readthedocs.io/en/latest/user/...
https://stackoverflow.com/ questions/25228106/how-t...
find your picture with it and save it - even in the base, even in the ace...
By the way, dokh is a zip set of xml folders, and it's even easier to pull everything out if you know how...

D
Dmitry, 2018-04-05
@demon416nds

it will not take off anywhere except the donkey
, and then the old one,
why engage in perversions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question