Page 1 of 3

Insert Pics plugin

Posted: 04.09.2023 06:22
by SamC
Insert Pics is very useful to make a quick record, when I need to share it with someone else, a doc with the pictures would be more directly.
I want to write code to convert the file to *.docx using py-docx package, there are several questions:
1.the *.cuda-pic file seems json format?
2."crc": 4258111033, what dose this key mean?
3.Any API to convert "pic_data" to form a picture again (and then using the pydocx add_pic method to insert it) ?
Thanks!

Re: About Insert Pics plugins

Posted: 04.09.2023 06:35
by main Alexey
1. yes.
2. it is control-sum of the picture. for details, see the python code. it is usually ignored because noone changes the Base64 code.

Code: Select all

def get_file_crc(filename):
    s = open(filename, "rb").read()
    return zlib.crc32(s) & 0xFFFFFFFF
3. data is Base64 encoded. see the source code, it is usual base64.

Code: Select all

def write_file_code(filename, s):
    s = s.encode()
    s = base64.b64decode(s)
    with open(filename, "wb") as f:
        f.write(s)

Re: About Insert Pics plugins

Posted: 04.09.2023 07:36
by main Alexey
for the DOCX idea. i welcome the addition, ie patch. you may fork the repo and do the patch. https://github.com/CudaText-addons/cuda_insert_pics

Re: About Insert Pics plugins

Posted: 04.09.2023 09:45
by SamC
Thanks, I will try that~

Re: About Insert Pics plugins

Posted: 05.09.2023 05:59
by SamC
Hello,
I've writen the code, it works when running outside. When add the code into insert-pic plugin. Then command still works, but Cuda crashed after that. With an error of "access violation" msg box flashed. Attached is the added code in insert-pic plugin _ini_.py
I can't figure it out. Please help me to check it.
BTW, It's hard for me to fork a repo and pull request recently, if the idea is useful for Cuda, maybe you can add it at sometime. Thanks~
sample.zip
(800 Bytes) Downloaded 159 times

Re: About Insert Pics plugins

Posted: 05.09.2023 06:17
by main Alexey
before I try the code, let's adapt it.

1) make separate module make_doc.py, which will expose public function "convert_to_doc(ed)". maybe add more parameters, for example the folder of DOCX file.

1b) class Command is not needed in your module.

2) reuse "tempfile" module in your code.

import tempfile
temp_filename = tempfile.gettempdir()+os.sep+'mypic.png'

do not use "disk D:" paths. I use linux.

Re: About Insert Pics plugins

Posted: 05.09.2023 06:30
by main Alexey
I tried to do it. but 'docx.py' (I get it from PYPI.org) shows errors:

Code: Select all

  File "/home/user/.config/cudatext/py/cuda_insert_pics/docx.py", line 30, in <module>
    from exceptions import PendingDeprecationWarning
ModuleNotFoundError: No module named 'exceptions'
ERROR: Exception in CudaText for export_docx: ModuleNotFoundError: No module named 'exceptions'

Re: About Insert Pics plugins

Posted: 05.09.2023 06:41
by SamC
I don't sure if understand your description.
For me, as the code needs python-docx package, I install it by PIP. As python3.8 is used in Cuda, I install python3.8 and copy several packages generated after pip command to the Cuda py folder. (the packages for python3.9 didn't work)
The copied packages included 3 items, there are some dependancy there:
20230905143705.png
20230905143705.png (1.52 KiB) Viewed 5242 times
:

Thanks for the notes to adapt code. That make the code more pythonic~

Re: About Insert Pics plugins

Posted: 05.09.2023 06:49
by main Alexey
yes, I don't need to download from PYPI.org, I need to install 'python-docx'. I still get exceptions inside exporter code. which I cannot fix. so, I request the full plugin code which is working.

Re: About Insert Pics plugins

Posted: 05.09.2023 07:03
by SamC
cuda_insert_pics.zip
(3.05 KiB) Downloaded 186 times
That's the original code that works(docx generated but Cuda crashed)

I haven't adapt it yet according to your guidance.