Package: docx-form
docx-formAPI Documentation|PyPi PageDescriptionThis package allows you to easily modify the values of content controls & form fields in.docxfiles with Python.Supported Content ControlsPlain TextRich TextDrop Down ListCombo BoxDate PickerCheck BoxSupported Form Fields (To-Do)TextDrop DownCheck BoxInstallationInstallPython(minimum version: 3.10.6)Warning: Python 3.11 breaks some dependencies as of 10/28/2022. This warning will be removed when this is no longer the case.In a terminal, run:pip install docx-formUsageThe file./docx_form_tests/test.docxwil be used for the following guide.Create a new python fileexample.pyImport docx-form:fromdocx_formimportDocxFormInitialize aDocxForminstance:...full_path='path/to/docx/file/test.docx'document=DocxForm(full_path)Print the content controls:...document.print_all_content_controls_and_form_fields()View the console output & make note of the indexes:0: RichTextContentControl | id: -1012910137 | text: Rich Text Content Control
1: RichTextContentControl | id: -1135860421 | text: Another Rich Text Content Control
2: PlainTextContentControl | id: -132710284 | text: Plain Text Content Control
3: PlainTextContentControl | id: 28152470 | text: Another Plain Text Content Control
4: CheckBoxContentControl | id: -1942055255 | text: ☐
5: CheckBoxContentControl | id: -635946620 | text: ☒
6: ComboBoxContentControl | id: 199831773 | text: Combo t Option 1
7: ComboBoxContentControl | id: -1984237200 | text: Choose an item.
8: DropDownListContentControl | id: -827207619 | text: Drop-Down Content Control
9: DropDownListContentControl | id: 2026666311 | text: Another Drop-Down Content Control
10: DatePickerContentControl | id: 645172330 | text: 9/6/2022
11: DatePickerContentControl | id: 539787165 | text: 9/7/2022Note: The Content Controls are listed starting from the top-left of the document going from left to right on each line of the document all the way to the bottom.Edit the second Rich Text Content Control:...# Import type for proper intellisense in your editor/IDEfromdocx_form.content_controlsimportRichTextContentControl...rich_text_control:RichTextContentControl=document.content_control_forms_and_form_fields[1]rich_text_control.set_text("The example worked!")Save the file:...# Note: This will overwrite the original filedocument.save()Ifdocument.print_all_content_controls_and_form_fields()is run again:...
1: RichTextContentControl | id: -1135860421 | text: The example worked!
...Full File:fromdocx_formimportDocxFormfromdocx_form.content_controlsimportRichTextContentControl# Create a DocxForm instancefull_path="path/to/docx/file/test.docx"document=DocxForm(full_path)# Kept for reference# document.print_all_content_controls_and_form_fields()# Edit the second Rich Text content controlrich_text_control:RichTextContentControl=(document.content_control_forms_and_form_fields[1])rich_text_control.set_text("The example worked!")# Note: This will overwrite the original filedocument.save()ExamplesThe file./docx_form_tests/test.docxwil be used for the following examples.Plain Textfromdocx_formimportDocxFormfromdocx_form.content_controlsimportPlainTextContentControl# Create a DocxForm instancefull_path="path/to/docx/file/test.docx"document=DocxForm(full_path)# Kept for reference# document.print_all_content_controls_and_form_fields()# Edit the content control (remember the console output)content_control:PlainTextContentControl=(document.content_control_forms_and_form_fields[2])content_control.set_text("Plain text edit")# Note: This will overwrite the original filedocument.save()Check Boxfromdocx_formimportDocxFormfromdocx_form.content_controlsimportCheckBoxContentControl# Create a DocxForm instancefull_path="path/to/docx/file/test.docx"document=DocxForm(full_path)# Kept for reference# document.print_all_content_controls_and_form_fields()# Edit the content control (remember the console output)content_control:CheckBoxContentControl=(document.content_control_forms_and_form_fields[4])content_control.set_check_box(True)# Note: This will overwrite the original filedocument.save()Combo Boxfromdocx_formimportDocxFormfromdocx_form.content_controlsimportComboBoxContentControl# Create a DocxForm instancefull_path="path/to/docx/file/test.docx"document=DocxForm(full_path)# Kept for reference# document.print_all_content_controls_and_form_fields()# Edit the content control (remember the console output)content_control:ComboBoxContentControl=(document.content_control_forms_and_form_fields[6])content_control.print_options()"""Output:0: Display Value = "None" || Value = "Choose an item."1: Display Value = "Combo Box Option 1" || Value = "Combo Box Option 1"2: Display Value = "Combo Box Option 2" || Value = "Combo Box Option 2""""content_control.set_text(2)# Note: This will overwrite the original filedocument.save()Drop Down Listfromdocx_formimportDocxFormfromdocx_form.content_controlsimportDropDownListContentControl# Create a DocxForm instancefull_path="path/to/docx/file/test.docx"document=DocxForm(full_path)# Kept for reference# document.print_all_content_controls_and_form_fields()# Edit the content control (remember the console output)content_control:DropDownListContentControl=(document.content_control_forms_and_form_fields[8])content_control.print_options()"""Output:0: Display Value = "None" || Value = "Choose an item."1: Display Value = "Drop-Down Content Control" || Value = "Drop-Down Content Control""""content_control.set_option(0)# Note: This will overwrite the original filedocument.save()Date Pickerfromdatetimeimportdatetimefromdocx_formimportDocxFormfromdocx_form.content_controlsimportDatePickerContentControl# Create a DocxForm instancefull_path="path/to/docx/file/test.docx"document=DocxForm(full_path)# Kept for reference# document.print_all_content_controls_and_form_fields()# Edit the content control (remember the console output)content_control:DatePickerContentControl=(document.content_control_forms_and_form_fields[10])print(content_control.date_format)# Output: "M/d/yyyy"new_date=datetime(1999,12,31)content_control.set_date(new_date)# Note: This will overwrite the original filedocument.save()Setup For ContributionRequirements:Python(minimum version: 3.10.6)Warning: Python 3.11 breaks some dependencies as of 10/28/2022. This warning will be removed when this is no longer the case.PoetryDO NOTforget to add Poetry to yourPATH(step 3)Environment SetupThe following will be done in Visual Studio Code on a Windows machine:Open a terminal in the repository's rootRunpoetry installRunpoetry env infoCopy the path listed underVirtualenv > Executable:Change the Python interpreter to the newly created Poetry virtual environment:CTRL + SHIFT + Pto open the command menuTypeinterpreterand hitENTERSelect+ Enter interpreter path...Paste the path to the virtual environmentHitENTEROpen./docx_form/docx_form.py(This is the root file, so do any local testing within theif __name__ == "__main__":scope)Click theRunbutton in the top-right of the editor (assuming thePython Extensionis installed)If no errors are thrown, you are ready to execute locally and using poetry!Open aGit Bashterminal in the repository's rootRuncurl -o- https://raw.githubusercontent.com/tapsellorg/conventional-commits-git-hook/master/scripts/install.sh | shHere is the repository for the git hook:conventional-commits-git-hookRungit initYou now have the git hook installed to ensure proper commit messagesFollowthese guidelinesfor commit messagesCode Style GuideDo your best to followPEP8 GuidelinesALLcode must haveType HintsIf a variable, parameter, or function return has a type ofAny, itWILL NOTbe accepted