office writer
This commit is contained in:
parent
710dc8ea47
commit
0fd38ea70c
4 changed files with 65 additions and 19 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import os
|
||||
import re
|
||||
import mimetypes
|
||||
import shutil
|
||||
|
|
@ -6,8 +7,9 @@ from olefile import OleFileIO
|
|||
|
||||
NAME = 'office'
|
||||
TYPES = ['.doc', '.xls', '.ppt', '.docx', '.xlsx', '.pptx', '.odt', '.ods', '.odp']
|
||||
RE_ODT_CTX = r"<meta:keyword>((.*))<\/meta:keyword>"
|
||||
RE_ODT_NOTE = r"<dc:description>((.*))<\/dc:description>"
|
||||
RE_ODT_CTX = r"(?P<pre><meta:keyword>)(?P<body>.*?)(?P<post><\/meta:keyword>)"
|
||||
RE_DOCX_CTX = r"(?P<pre><cp:keywords>)(?P<body>.*?)(?P<post><\/cp:keywords>)"
|
||||
REGEX = r"(?P<pre><dc:description>)(?P<body>.*?)(?P<post><\/dc:description>)"
|
||||
|
||||
# Office Document processing inspired by
|
||||
# https://github.com/kappapiana/anonymize/blob/master/anonymize.py
|
||||
|
|
@ -28,6 +30,18 @@ def unzip_file(orig_file, tmp_dir):
|
|||
else:
|
||||
return type[0]
|
||||
|
||||
def get_context(meta_file, re_ctx):
|
||||
tags = note = []
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.search(re_ctx, meta_content)
|
||||
if tagsearch:
|
||||
tags = tagsearch.group('body').split(' ')
|
||||
notesearch = re.search(REGEX, meta_content)
|
||||
if notesearch:
|
||||
note = [notesearch.group('body')]
|
||||
return [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
|
||||
|
||||
def reader(o):
|
||||
tmp_folder = Path('unzip__' + o.file.stem)
|
||||
file_type = unzip_file(o.file, tmp_folder)
|
||||
|
|
@ -36,14 +50,7 @@ def reader(o):
|
|||
# OpenOffice / LibreOffice / Open Document Format
|
||||
if file_type in ['odt', 'ods', 'odp']:
|
||||
meta_file = tmp_folder / 'meta.xml'
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.findall(RE_ODT_CTX, meta_content)
|
||||
if tagsearch:
|
||||
tags = list(set(tagsearch[0]))
|
||||
notesearch = re.findall(RE_ODT_NOTE, meta_content)
|
||||
if notesearch:
|
||||
note = list(set(notesearch[0]))
|
||||
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
argv = get_context(meta_file, RE_ODT_CTX)
|
||||
context = o.context(argv)
|
||||
shutil.rmtree(tmp_folder)
|
||||
return context
|
||||
|
|
@ -51,14 +58,7 @@ def reader(o):
|
|||
# Microsoft Office Format
|
||||
elif file_type in ['docx', 'xlsx', 'pptx']:
|
||||
meta_file = tmp_folder / 'docProps' / 'core.xml'
|
||||
meta_content = meta_file.read_text()
|
||||
tagsearch = re.findall(RE_ODT_CTX, meta_content)
|
||||
if tagsearch:
|
||||
tags = list(set(tagsearch[0]))
|
||||
notesearch = re.findall(RE_ODT_NOTE, meta_content)
|
||||
if notesearch:
|
||||
note = list(set(notesearch[0]))
|
||||
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
|
||||
argv = get_context(meta_file, RE_DOCX_CTX)
|
||||
context = o.context(argv)
|
||||
shutil.rmtree(tmp_folder)
|
||||
return context
|
||||
|
|
@ -74,4 +74,40 @@ def reader(o):
|
|||
note = [meta.comments.decode('UTF-8')]
|
||||
argv = [x for x in tags + note if x != '']
|
||||
context = o.context(argv)
|
||||
return context
|
||||
return context
|
||||
|
||||
|
||||
def writer(o):
|
||||
string = ' '.join([x for x in o.collect(o.context) if x != None])
|
||||
tmp_folder = Path('unzip__' + o.file.stem)
|
||||
file_type = unzip_file(o.file, tmp_folder)
|
||||
regex = re.compile(REGEX)
|
||||
string = f"\\g<pre>{string}\\g<post>"
|
||||
|
||||
# OpenOffice / LibreOffice / Open Document Format
|
||||
if file_type in ['odt', 'ods', 'odp']:
|
||||
meta_file = tmp_folder / 'meta.xml'
|
||||
with meta_file.open('r+') as file:
|
||||
content = regex.sub(string, file.read())
|
||||
file.seek(0, 0)
|
||||
file.write(content)
|
||||
shutil.make_archive(o.file, "zip", tmp_folder)
|
||||
output_file_zip = str(o.file) + ".zip"
|
||||
os.rename(output_file_zip, o.file)
|
||||
shutil.rmtree(tmp_folder)
|
||||
|
||||
# Microsoft Office Format
|
||||
elif file_type in ['docx', 'xlsx', 'pptx']:
|
||||
meta_file = tmp_folder / 'docProps' / 'core.xml'
|
||||
with meta_file.open('r+') as file:
|
||||
content = regex.sub(string, file.read())
|
||||
file.seek(0, 0)
|
||||
file.write(content)
|
||||
shutil.make_archive(o.file, "zip", tmp_folder)
|
||||
output_file_zip = str(o.file) + ".zip"
|
||||
os.rename(output_file_zip, o.file)
|
||||
shutil.rmtree(tmp_folder)
|
||||
|
||||
# Legacy Microsoft Office Format
|
||||
elif file_type in ['application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint']:
|
||||
print('unsupported file format')
|
||||
|
|
|
|||
|
|
@ -6,6 +6,16 @@ config:
|
|||
projects: album
|
||||
tags: genre
|
||||
items:
|
||||
office.odt:
|
||||
contexts: []
|
||||
projects:
|
||||
- orvani
|
||||
tags: []
|
||||
sheet.xlsx:
|
||||
contexts: []
|
||||
projects:
|
||||
- orvani
|
||||
tags: []
|
||||
sub:
|
||||
contexts:
|
||||
- folder
|
||||
|
|
|
|||
BIN
test/office.odt
BIN
test/office.odt
Binary file not shown.
BIN
test/sheet.xlsx
BIN
test/sheet.xlsx
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue