Skip to content

Commit

Permalink
fix: docx export (rbz)
Browse files Browse the repository at this point in the history
  • Loading branch information
drfho committed Jun 4, 2024
1 parent 82ab3dc commit 715f62e
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions Products/zms/import/docx.metacmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import re
import shutil
import tempfile
import urllib
from Products.zms import standard
env = {}
dpi = 3000
Expand Down Expand Up @@ -53,7 +54,7 @@ def process_images(docbookxml):
q = pq[1]
r = re.compile(p)
for f in r.findall(docbookxml):
d = dict(re.findall('\\s(.*?)="(.*?)"',f))
d = dict(re.findall(r'\s(.*?)="(.*?)"',f))
read_image(d)
index = len(images)
name = d['src'].split('/')[-1]
Expand Down Expand Up @@ -190,7 +191,7 @@ regions = {
def filter_regions(vtype):
# Py-2.7: {k: v for k, v in regions.iteritems() if v['type']==vtype}
d = {}
[operator.setitem(d,x,regions[x]) for x in list(region) if regions[x]['type']==vtype]
[operator.setitem(d,x,regions[x]) for x in list(regions) if regions[x]['type']==vtype]
return d
def process_regions(html,):
Expand Down Expand Up @@ -294,7 +295,7 @@ def process_paragraphs(html):
return docbookxml
def html2docbook(html):
r = re.compile('<(.*?)>')
r = re.compile(r'<(.*?)>')
for f in r.findall(html):
n = f
if n.find(' ') > 0:
Expand All @@ -307,23 +308,23 @@ def html2docbook(html):
html = html.replace('<%s>'%f,'')
elif regions[n]['type']=='drawing':
html = html.replace('<%s>'%f,'[[%s]]'%f)
html = re.sub('<a .*?href="javascript:.*?".*?>((\\S|\\s)*?)<\\/a>','\\1',html)
html = re.sub('<!--(.*?)-->','',html)
html = re.sub('&nbsp;',' ',html)
html = re.sub('&sect;','§',html)
html = re.sub('&(.*?);','&amp;\\1;',html)
html = re.sub(r'<a .*?href="javascript:.*?".*?>((\S|\s)*?)<\/a>','\\1',html)
html = re.sub(r'<!--(.*?)-->','',html)
html = re.sub(r'&nbsp;',' ',html)
html = re.sub(r'&sect;','§',html)
html = re.sub(r'&(.*?);','&amp;\\1;',html)
html = html.replace('\n',' ')
html = html.replace('\r',' ')
html = html.replace('\t',' ')
while html.find(' ')>=0:
html = html.replace(' ',' ')
docbookxml = process_paragraphs(html)
docbookxml = process_paragraphs(unicode(html).encode('utf-8'))
docbookxml = process_images(docbookxml)
return docbookxml
def manage_exportDocx( self):
request = self.REQUEST
tempfolder = tempfile.mktemp()
tempfolder = tempfile.mkdtemp()
request.set('URL',self.absolute_url())
env['self'] = self
env['server_url'] = request['SERVER_URL']
Expand Down Expand Up @@ -378,13 +379,13 @@ def manage_exportDocx( self):
bodyContent = []
bodyContent.append('<h1>%s</h1>'%self.getTitle(request))
bodyContent.append(self.getBodyContent(request))
bodyContent = ''.join(bodyContent)
bodyContent = ''.join([standard.pystr(s) for s in bodyContent])
document = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>%s</w:body>
</w:document>'''%html2docbook(bodyContent)
f = open(os.path.join(tempfolder,'word','document.xml'),'w')
f.write(document)
f.write(unicode(document).encode('utf-8'))
f.close()
# Numbering
Expand Down Expand Up @@ -437,7 +438,7 @@ def manage_exportDocx( self):
</item>
<item key="description"></item>
<item key="execution" type="int">0</item>
<item key="icon_clazz"><![CDATA[icon-download]]></item>
<item key="icon_clazz"><![CDATA[icon-download fas fa-download]]></item>
<item key="id"><![CDATA[manage_exportDocx]]></item>
<item key="meta_type"><![CDATA[External Method]]></item>
<item key="meta_types" type="list">
Expand All @@ -446,7 +447,7 @@ def manage_exportDocx( self):
</list>
</item>
<item key="name"><![CDATA[DOCX Export]]></item>
<item key="revision"><![CDATA[0.0.0]]></item>
<item key="revision"><![CDATA[4.0.1]]></item>
<item key="roles" type="list">
<list>
<item><![CDATA[ZMSAdministrator]]></item>
Expand Down Expand Up @@ -479,7 +480,7 @@ def manage_importDocx( self):
rc.append(nd)
return ''.join(rc).strip()
result = []
tempfolder = tempfile.mktemp()
tempfolder = tempfile.mkdtemp()
f = request['file']
filename = os.path.join(tempfolder,f.filename)
_fileutil.exportObj(f,filename)
Expand Down Expand Up @@ -507,12 +508,12 @@ def manage_importDocx( self):
html += '<label class="control-label col-sm-2" for="file"><span>%s</span></label>'%self.getZMILangStr('ATTR_FILE')
html += '<div class="col-sm-10"><input class="btn btn-file" size="25" name="file" type="file"/></div>'
html += '</div><!-- .form-group -->'
html += '<div class="form-row">'
html += '<div class="form-group row">'
html += '<div class="controls save">'
html += '<button type="submit" name="btn" class="btn btn-primary" value="%s">%s</button> '%(self.getZMILangStr('BTN_IMPORT'),self.getZMILangStr('BTN_IMPORT'))
html += '<button type="button" name="btn" class="btn btn-secondary" value="return %s" onclick="btn_execute_click()">%s</button> '%(self.getZMILangStr('BTN_EXECUTE'),self.getZMILangStr('BTN_EXECUTE'))
html += '</div>'
html += '</div><!-- .form-row -->'
html += '</div><!-- .form-group -->'
# ---------------------------------
Expand All @@ -526,7 +527,7 @@ def manage_importDocx( self):
</item>
<item key="description"></item>
<item key="execution" type="int">0</item>
<item key="icon_clazz"><![CDATA[icon-upload]]></item>
<item key="icon_clazz"><![CDATA[icon-upload fas fa-upload]]></item>
<item key="id"><![CDATA[manage_importDocx]]></item>
<item key="meta_type"><![CDATA[External Method]]></item>
<item key="meta_types" type="list">
Expand Down

0 comments on commit 715f62e

Please sign in to comment.