Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
docx2python/docx2python/forms.py at master · LectronPusher/docx2python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
LectronPusher
/
docx2python
Public
forked from
ShayHill/docx2python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
docx2python
/
docx2python
/
forms.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
89 lines (67 loc) · 3.08 KB
master
Breadcrumbs
docx2python
/
docx2python
/
forms.py
Copy path
Top
File metadata and controls
Code
Blame
89 lines (67 loc) · 3.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
""" Form checkboxes, dropdowns, and other non-text elements visible in Word.
:author: Shay Hill
:created: 6/17/2020
Word represents some special characters as non-text elements (e.g., checkBox). These
functions examine these elements to infer suitable text replacements.
This file references "
\u2610
" and "
\u2612
" a few times. These are open and
crossed-out checkboxes. Pypi doesn't like them in my file, so I have to reference
them by their escape sequences.
"""
from
contextlib
import
suppress
from
typing
import
Union
from
lxml
.
etree
import
_Element
as
EtreeElement
# type: ignore
from
.
namespace
import
qn
def
get_checkBox_entry
(
checkBox
:
EtreeElement
)
->
str
:
"""Create text representation for a checkBox element.
:param checkBox: a checkBox xml element
:return:
1. attempt to get ``checked.w:val`` and return "
\u2610
" or "
\u2612
"
2. attempt to get ``default.w:val`` and return "
\u2610
" or "
\u2612
"
3. return ``--checkbox failed--``
Docx xml has at least two types of checkbox elements::
1. ``checkBox`` can only be checked when the form is locked. These do not
contain a text element, so this function is needed to select one from the
``w:checked`` or ``w:default`` sub-elements.
2. ``checkbox`` can be checked any time. Prints text as "
\u2610
" or "
\u2612
".
Docx2Python can safely ignore this second type, as there will be a <w:t>
element inside with a checkbox character.
<w:checkBox>
<w:sizeAuto/>
<w:default w:val="1"/>
<w:checked w:val="0"/>
</w:checkBox>
If the ``checked`` attribute is absent, return the default
If the ``checked`` attribute is present, but not w:val is given, return unchecked
"""
def
get_wval
()
->
Union
[
str
,
None
]:
"""Get the value of the ``w:val`` attribute of the ``checked`` element.
:return: the value of the ``w:val`` attribute of the ``checked`` element
"""
with
suppress
(
StopIteration
):
checked
=
next
(
checkBox
.
iterfind
(
qn
(
"w:checked"
)))
return
str
(
checked
.
attrib
.
get
(
qn
(
"w:val"
))
or
"1"
)
with
suppress
(
StopIteration
,
KeyError
):
default
=
next
(
checkBox
.
iterfind
(
qn
(
"w:default"
)))
return
str
(
default
.
attrib
[
qn
(
"w:val"
)])
return
None
return
{
"0"
:
"
\u2610
"
,
"1"
:
"
\u2612
"
,
None
:
"----checkbox failed----"
}[
get_wval
()]
def
get_ddList_entry
(
ddList
:
EtreeElement
)
->
str
:
"""Get only the selected string of a dropdown list.
:param ddList: a dropdown-list element
:return: w:listEntry value of input element.
<w:ddList>
<w:result w:val="1"/>
<w:listEntry w:val="selection 1"/>
<w:listEntry w:val="selection 2"/>
</w:ddList>
<w:result w:val="0"/> might be missing when selection is "0"
"""
list_entries
=
[
x
.
attrib
.
get
(
qn
(
"w:val"
))
for
x
in
ddList
.
findall
(
qn
(
"w:listEntry"
))
]
try
:
result
=
next
(
ddList
.
iterfind
(
qn
(
"w:result"
)))
list_index
=
int
(
result
.
attrib
[
qn
(
"w:val"
)])
except
(
StopIteration
,
KeyError
):
list_index
=
0
return
str
(
list_entries
[
list_index
])
You can’t perform that action at this time.