I’m having a peculiar issue, and I’m wondering how to fix the mess i’ve made with my keynote file
I was attempting to alter my keynote file using a short python script, but the lines that I altered are not being read by Keynote Manager. When opening the file in the manager, I can see the divisions (unaltered by my script) but none of the keynotes.
Basically, I was attempting to append the text values of the keynotes with the key value (e.g. “{key value}; {text description}”) by splitting each line containing a keynote by its tabs and reformatting it using the following:
formatted_line = f"{key}\t{key}; {txt}\t{pkey}\n"
Opening the file in notepad I don’t see anything wrong with the new keynotes, but when viewing the altered file in notepad++ I see previously absent zero-width characters (e.g. “ZWSBSP”) in place of my ‘\n’ characters that I suspect are causing my problem, but I’m not sure where I went wrong in my script. I am explicitly opening and writing to the files using encoding=‘utf-16’. It’s possible that my problem is caused by the environment i’m running the script in (I am using the “RevitPythonShell” plugin from architecture-building-systems on github, it might be utilizing an older version of Unicode, I think I saw mention of some zero-width characters depreciated) Here’s the script i’m attempting to use that’s causing the problem:
import shutil
import os
print('Begin script.....')
file = "<path_to_keynote_file"
backup = "<path_to_backup_file"
# Check if the original file exists
if not os.path.exists(file):
print(f"Error: The file '{file}' does not exist.")
else:
# Try to create a backup
try:
shutil.copyfile(file, backup)
print(f"Backup successfully created at '{backup}'.")
except Exception as e:
print(f"An error occurred while creating the backup: {e}")
def append_key(line):
#Skip lines that begin with '#' or 'DIVISION'
if line.startswith('#') or line.startswith('DIVISION'):
return line
#Split lines by tabs
parts = line.split('\t')
#Ensure we have exactly 3 parts
if len(parts) !=3:
return None
print('Something went wrong with splitting the line')
#extract components
key = parts[0].strip()
txt = parts[1].strip()
pkey = parts[2].strip()
#Format the new line
formatted_line = f"{key}\t{key}; {txt}\t{pkey}\n"
return formatted_line
with open(backup, "r", encoding="utf-16") as infile, open(file, 'w', encoding="utf-16") as outfile:
for line in infile:
processed_line = append_key(line)
if processed_line:
outfile.write(processed_line)
print('Completed!')
Any help would be appreciated!