Skip to content

Instantly share code, notes, and snippets.

@yiyuezhuo
Created September 12, 2024 09:13
Show Gist options
  • Save yiyuezhuo/86ce4ccbf823abb6364f8caf0571fbf0 to your computer and use it in GitHub Desktop.
Save yiyuezhuo/86ce4ccbf823abb6364f8caf0571fbf0 to your computer and use it in GitHub Desktop.
Increase outline level
"""
<w:outlineLvl w:val="4"/>
"""
import re
def transform(text: str, inc: int):
return re.sub(r'w:outlineLvl w:val="(\d)"', lambda exp: f'w:outlineLvl w:val="{int(exp.groups()[0]) + inc}"', text)
# return re.sub(r'w:pStyle w:val="(\d)"', lambda exp: f'w:pStyle w:val="{int(exp.groups()[0])+inc}"', text)
zip_doc_path = "word/document.xml"
def read_document(doc_path: str) -> str:
with zipfile.ZipFile(doc_path, "r") as zin:
b = zin.read(zip_doc_path)
return b.decode('utf-8')
# https://stackoverflow.com/questions/25738523/how-to-update-one-file-inside-zip-file
import os
import zipfile
import tempfile
def updateZip(zipname, filename, data):
# generate a temp file
tmpfd, tmpname = tempfile.mkstemp(dir=os.path.dirname(zipname))
os.close(tmpfd)
# create a temp copy of the archive without filename
with zipfile.ZipFile(zipname, 'r') as zin:
with zipfile.ZipFile(tmpname, 'w') as zout:
zout.comment = zin.comment # preserve the comment
for item in zin.infolist():
if item.filename != filename:
zout.writestr(item, zin.read(item.filename))
# replace with the temp archive
os.remove(zipname)
os.rename(tmpname, zipname)
# now add filename with its new data
with zipfile.ZipFile(zipname, mode='a', compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr(filename, data)
# msg = 'This data did not exist in a file before being added to the ZIP file'
# updateZip('1.zip', 'index.html', msg)
def apply_inc_to_docx(doc_path: str, inc: int):
xml = read_document(doc_path)
xml_transformed = transform(xml, inc)
updateZip(doc_path, zip_doc_path, xml_transformed)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("doc_path")
parser.add_argument("inc", type=int)
args = parser.parse_args()
apply_inc_to_docx(args.doc_path, args.inc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment