mercredi 19 janvier 2022

Could python be able to change the checkbox value in Microsoft Word

I could replace the key into value in a Word template using below function.

def replace_text_in_paragraph(paragraph, key, value):
if key in paragraph.text:
    inline = paragraph.runs
    # Replace strings and retain the same style.
    # The text to be replaced can be split over several runs so
    # search through, identify which runs need to have text replaced
    # then replace the text in those identified
    started = False
    search_index = 0
    # found_runs is a list of (inline index, index of match, length of match)
    found_runs = list()
    found_all = False
    replace_done = False

    for i in range(len(inline)):
        # case 1: found in single run so short circuit the replace
        if key in inline[i].text and not started:
            found_runs.append((i, inline[i].text.find(key), len(key)))
            text = inline[i].text.replace(key, str(value))
            inline[i].text = text
            replace_done = True
            found_all = True
            break
        if key[search_index] not in inline[i].text and not started:
            # keep looking ...
            continue

        # case 2: search for partial text, find first run
        if key[search_index] in inline[i].text and inline[i].text[-1] in key and not started:
            # check sequence
            start_index = inline[i].text.find(key[search_index])
            check_length = len(inline[i].text)
            for text_index in range(start_index, check_length):
                if inline[i].text[text_index] != key[search_index]:
                    # no match so must be false positive
                    break
            if search_index == 0:
                started = True
            chars_found = check_length - start_index
            search_index += chars_found
            found_runs.append((i, start_index, chars_found))
            if search_index != len(key):
                continue
            else:
                # found all chars in key
                found_all = True
                break

        # case 3: search for partial text, find subsequent run
        if key[search_index] in inline[i].text and started and not found_all:
            # check sequence
            chars_found = 0
            check_length = len(inline[i].text)
            # print(check_length)
            for text_index in range(0, check_length):
                # print(key[search_index])
                if inline[i].text[text_index] == key[search_index]:
                    search_index += 1
                    chars_found += 1
                else:
                    break
            # no match so must be end
            found_runs.append((i, 0, chars_found))
            if search_index == len(key):
                found_all = True
                break

    if found_all and not replace_done:
        for i, item in enumerate(found_runs):
            index, start, length = [t for t in item]
            if i == 0:
                text = inline[index].text.replace(
                    inline[index].text[start:start + length], str(value))
                inline[index].text = text
            else:
                text = inline[index].text.replace(
                    inline[index].text[start:start + length], '')
                inline[index].text = text

But how could I change the checkbox value.
If Word could not, be open to suggest a fillable pdf as when you create a fillable checkbox in pdf it can be given a JavaScript ID to this checkbox. But I do not know how to achieve that. That is all I can come up with.

Any help will be appreciated.




Aucun commentaire:

Enregistrer un commentaire