Skip to content

Instantly share code, notes, and snippets.

@Romern
Created February 28, 2026 08:47
Show Gist options
  • Select an option

  • Save Romern/eaa2647c4553ea8c9fdd1dbe4dd0cb85 to your computer and use it in GitHub Desktop.

Select an option

Save Romern/eaa2647c4553ea8c9fdd1dbe4dd0cb85 to your computer and use it in GitHub Desktop.
Remove pre-rendered versions of images and subdocuments in LibreOffice Documents
import zipfile
import re
import sys
"""
<draw:image xlink:href="Pictures/1000000100000538000005385B1FE860.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" draw:mime-type="image/png"/>
<draw:image xlink:href="./ObjectReplacements/Object 1" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>
"""
regex = re.compile('<draw:image[^>]*(png|ObjectReplacements)[^>]*>')
def remove_pattern_from_zip(input_zip, output_zip):
with zipfile.ZipFile(input_zip, 'r') as zip_in:
with zipfile.ZipFile(output_zip, 'w') as zip_out:
for item in zip_in.infolist():
if 'ObjectReplacements' in item.filename or '.png' in item.filename:
continue
content = zip_in.read(item.filename)
if item.filename.endswith('.xml'):
text_content = content.decode('utf-8')
modified_content = regex.sub('', text_content)
zip_out.writestr(item, modified_content.encode('utf-8'))
else:
zip_out.writestr(item, content)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python remove_thumbnails.py <input_zip> <output_zip>")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
remove_pattern_from_zip(input_file, output_file)
print(f"Processed zip saved to: {output_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment