Skip to content

Instantly share code, notes, and snippets.

@cbonesana
Created November 17, 2019 13:43
Show Gist options
  • Save cbonesana/ffe011e068e7a2dab0284ddb4c7ce8c4 to your computer and use it in GitHub Desktop.
Save cbonesana/ffe011e068e7a2dab0284ddb4c7ce8c4 to your computer and use it in GitHub Desktop.
import logging
import os
import sys
import lxml.etree as etree
from openpyxl import load_workbook
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def append_something(filename, what):
return "{0}_{2}{1}".format(*os.path.splitext(filename) + (what,))
def paranid_story_lines(xml, row):
d = row[3]
f = row[5]
d_text = d.value
if type(d_text) is int:
nodes = xml.xpath(f'//t[@id="{d_text}"]')
if len(nodes) > 0:
f.value = nodes[0].text
def paranid_sink_lines(xml, row):
d = row[3]
f = row[5]
d_text = d.value
if type(d_text) is int:
nodes = xml.xpath(f'//t[@id="{d_text}"]')
if len(nodes) > 0:
f.value = nodes[0].text
def split_story_lines(xml, row):
c = row[2]
d = row[3]
f = row[5]
c_text = c.value
d_text = d.value
if not c_text:
return
if type(d_text) is int and '"' in c_text:
intro = c_text.split('"')[1]
nodes = xml.xpath(f'//t[@id="{intro}{d_text:03}"]')
if len(nodes) > 0:
f.value = nodes[0].text
def main(args):
path_xlsx_in = args[1]
path_xlsx_out = append_something(path_xlsx_in, 'it')
path_xml = args[2]
logging.info(f'XML file as input: {path_xml}')
logging.info(f'XLSX file as input: {path_xlsx_in}')
logging.info(f'XLSX file as output: {path_xlsx_out}')
# if not os.path.exists(path_xlsx_out):
# copyfile(path_xlsx_in, path_xlsx_out)
xml = etree.parse(path_xml)
wb = load_workbook(path_xlsx_in)
sheet_names = wb.sheetnames
function_sheet = [paranid_story_lines, paranid_sink_lines, split_story_lines]
for s in range(len(sheet_names)):
sheet_name = sheet_names[s]
sheet = wb[sheet_name]
f = function_sheet[s]
logging.info(sheet_name)
for r in sheet.rows:
f(xml, r)
wb.save(path_xlsx_out)
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment