1
0
mirror of synced 2024-11-25 18:54:22 +01:00

Display docx metadata

This commit is contained in:
jvoisin 2018-03-31 20:56:15 +02:00
parent e4d2506d6a
commit 1ee936420c
2 changed files with 14 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import re
import subprocess import subprocess
import json import json
import zipfile import zipfile
@ -16,10 +17,18 @@ class OfficeParser(abstract.AbstractParser):
files_to_keep = {'_rels/.rels', 'word/_rels/document.xml.rels'} files_to_keep = {'_rels/.rels', 'word/_rels/document.xml.rels'}
def get_meta(self): 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 = {} metadata = {}
zipin = zipfile.ZipFile(self.filename) zipin = zipfile.ZipFile(self.filename)
for item in zipin.namelist(): for item in zipin.namelist():
if item.startswith('docProps/'): 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' metadata[item] = 'harmful content'
zipin.close() zipin.close()
return metadata return metadata

View File

@ -42,7 +42,9 @@ class TestGetMeta(unittest.TestCase):
def test_docx(self): def test_docx(self):
p = office.OfficeParser('./tests/data/dirty.docx') p = office.OfficeParser('./tests/data/dirty.docx')
meta = p.get_meta() 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): class TestCleaning(unittest.TestCase):