Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
mat2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tails
mat2
Commits
1ee93642
Commit
1ee93642
authored
6 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Display docx metadata
parent
e4d2506d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/office.py
+11
-2
11 additions, 2 deletions
src/office.py
tests/test_libmat2.py
+3
-1
3 additions, 1 deletion
tests/test_libmat2.py
with
14 additions
and
3 deletions
src/office.py
+
11
−
2
View file @
1ee93642
import
re
import
subprocess
import
json
import
zipfile
...
...
@@ -16,11 +17,19 @@ class OfficeParser(abstract.AbstractParser):
files_to_keep
=
{
'
_rels/.rels
'
,
'
word/_rels/document.xml.rels
'
}
def
get_meta
(
self
):
"""
Yes, I know that parsing xml with regexp ain
'
t pretty,
be my guest and fix it if you want.
"""
metadata
=
{}
zipin
=
zipfile
.
ZipFile
(
self
.
filename
)
for
item
in
zipin
.
namelist
():
if
item
.
startswith
(
'
docProps/
'
):
metadata
[
item
]
=
'
harmful content
'
if
item
.
startswith
(
'
docProps/
'
)
and
item
.
endswith
(
'
.xml
'
):
content
=
zipin
.
read
(
item
).
decode
(
'
utf-8
'
)
for
(
key
,
value
)
in
re
.
findall
(
r
"
<(.+)>(.+)</\1>
"
,
content
,
re
.
I
):
metadata
[
key
]
=
value
if
not
metadata
:
# better safe than sorry
metadata
[
item
]
=
'
harmful content
'
zipin
.
close
()
return
metadata
...
...
This diff is collapsed.
Click to expand it.
tests/test_libmat2.py
+
3
−
1
View file @
1ee93642
...
...
@@ -42,7 +42,9 @@ class TestGetMeta(unittest.TestCase):
def
test_docx
(
self
):
p
=
office
.
OfficeParser
(
'
./tests/data/dirty.docx
'
)
meta
=
p
.
get_meta
()
print
(
meta
)
self
.
assertEqual
(
meta
[
'
cp:lastModifiedBy
'
],
'
Julien Voisin
'
)
self
.
assertEqual
(
meta
[
'
dc:creator
'
],
'
julien voisin
'
)
self
.
assertEqual
(
meta
[
'
Application
'
],
'
LibreOffice/5.4.5.1$Linux_X86_64 LibreOffice_project/40m0$Build-1
'
)
class
TestCleaning
(
unittest
.
TestCase
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment