Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_referenced_parts(self, src_part, dst_part, element):
rid_elements = xpath(element, './/*[@r:id]')
for rid_element in rid_elements:
rid = rid_element.get('{%s}id' % NS['r'])
rel = src_part.rels[rid]
if rel.reltype in REFERENCED_PARTS_IGNORED_RELTYPES:
continue
new_rel = self.add_relationship(src_part, dst_part, rel)
rid_element.set('{%s}id' % NS['r'], new_rel.rId)
anum_id = num_element.xpath('//w:abstractNumId')[0]
if anum_id.val not in self.anum_id_mapping:
# Find the referenced element
res = src_numbering_part.element.xpath(
'.//w:abstractNum[@w:abstractNumId="%s"]' % anum_id.val)
if not res:
continue
anum_element = deepcopy(res[0])
self.anum_id_mapping[anum_id.val] = next_anum_id
anum_id.val = next_anum_id
# anum_element.abstractNumId = next_anum_id
anum_element.set('{%s}abstractNumId' % NS['w'], str(next_anum_id))
# Make sure we have a unique nsid so numberings restart properly
nsid = anum_element.find('.//w:nsid', NS)
if nsid is not None:
nsid.set(
'{%s}val' % NS['w'],
"{0:08X}".format(int(10**8 * random.random()))
)
self._insert_abstract_num(anum_element)
else:
anum_id.val = self.anum_id_mapping[anum_id.val]
self._insert_num(num_element)
# Fix references
for num_id_ref in xpath(element, './/w:numId'):
num_id_ref.val = self.num_id_mapping.get(
num_id_ref.val, num_id_ref.val)
def add_footnotes(self, doc, element):
"""Add footnotes from the given document used in the given element."""
footnotes_refs = element.findall('.//w:footnoteReference', NS)
if not footnotes_refs:
return
footnote_part = doc.part.rels.part_with_reltype(RT.FOOTNOTES)
my_footnote_part = self.footnote_part()
footnotes = parse_xml(my_footnote_part.blob)
next_id = len(footnotes) + 1
for ref in footnotes_refs:
id_ = ref.get('{%s}id' % NS['w'])
element = parse_xml(footnote_part.blob)
footnote = deepcopy(element.find('.//w:footnote[@w:id="%s"]' % id_, NS))
footnotes.append(footnote)
def add_shapes(self, doc, element):
shapes = xpath(element, './/v:shape/v:imagedata')
for shape in shapes:
rid = shape.get('{%s}id' % NS['r'])
img_part = doc.part.rels[rid].target_part
new_img_part = self.pkg.image_parts._get_by_sha1(img_part.sha1)
if new_img_part is None:
image = ImageWrapper(img_part)
new_img_part = self.pkg.image_parts._add_image_part(image)
new_rid = self.doc.part.relate_to(new_img_part, RT.IMAGE)
shape.set('{%s}id' % NS['r'], new_rid)
def add(self, name, value):
"""Add a property."""
pids = [int(pid) for pid in xpath(self._element, u'.//cp:property/@pid')]
if pids:
pid = max(pids) + 1
else:
pid = MIN_PID
prop = parse_xml(''.format(NS['cp']))
prop.set('fmtid', CUSTOM_PROPERTY_FMTID)
prop.set('name', name)
prop.set('pid', text_type(pid))
value_el = value2vt(value)
prop.append(value_el)
self._element.append(prop)
self._update_part()