diff --git a/examples/custom_export.py b/examples/custom_export.py index 07ecd87..618a3ca 100644 --- a/examples/custom_export.py +++ b/examples/custom_export.py @@ -120,17 +120,18 @@ def create_files(api, tree, output_dir: Path): create_files(api, item.child_items, current_directory) for note in item.child_notes: - with open( - (current_directory / replacements(note.title)).with_suffix(".md"), "w" - ) as outfile: - outfile.write(note.body) + note_path = (current_directory / replacements(note.title)).with_suffix( + ".md" + ) + note_path.write_text(note.body, encoding="utf-8") for resource in item.child_resources: + # TODO: do this first and replace resource paths in the note body resource_binary = api.get_resource_file(id_=resource.id) - with open( - current_directory / replacements(resource.title or resource.id), "wb" - ) as outfile: - outfile.write(resource_binary) + resource_path = current_directory / replacements( + resource.title or resource.id + ) + resource_path.write_bytes(resource_binary) def main(): diff --git a/examples/note_tree_export.py b/examples/note_tree_export.py index ac367a0..ec3d639 100644 --- a/examples/note_tree_export.py +++ b/examples/note_tree_export.py @@ -183,7 +183,7 @@ def main(): help="Title of the root notebooks. By default all notebooks are selected.", ) parser.add_argument( - "--output", help="Path to the output file.", default="note_tree.txt" + "--output", type=Path, help="Path to the output file.", default="note_tree.txt" ) args = parser.parse_args() @@ -216,13 +216,14 @@ def main(): html = html.replace(f'href=":/{resource.id}"', "") elif resource.mime.startswith("image"): resource_binary = api.get_resource_file(resource.id) - resource_path = str(Path(tmpdirname) / resource.id) - with open(resource_path, "wb") as outfile: - outfile.write(resource_binary) + resource_path = Path(tmpdirname) / resource.id + resource_path.write_bytes(resource_binary) # Replace joplin's local link with the path to the just # downloaded resource. Use the "file:///" protocol: # https://stackoverflow.com/a/18246357/7410886 - html = html.replace(f":/{resource.id}", f"file:///{resource_path}") + html = html.replace( + f":/{resource.id}", f"file:///{str(resource_path)}" + ) # Replace remaining note links. # - Joplin ID: ":/" @@ -230,8 +231,7 @@ def main(): html = html.replace('href=":/', 'href="#') if output_format == ".html": - with open(args.output, "w") as outfile: - outfile.write(html) + args.output.write_text(html, encoding="utf-8") else: from weasyprint import CSS, HTML @@ -241,8 +241,7 @@ def main(): if output_format == ".txt": txt_tree = item_tree_to_txt(sorted_item_tree) - with open(args.output, "w") as outfile: - outfile.write(txt_tree) + args.output.write_text(txt_tree, encoding="utf-8") if __name__ == "__main__":