Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move PDF files to ZimWiki notes
import os
import pathlib
import shutil
'''
Move a PDF file from a designated folder to a ZimWiki notebook and create a
corresponding note that links to the PDF. The note can be used to summarize
the content of the file or provide other useful information about it. Clicking
on the link within the note opens the PDF file in the default PDF viewer. The
note can be moved anywhere within ZimWiki.
'''
def create_folder(file_name, notebook_dir):
folder_name = file_name.replace(" ","_")
# the folder name will be the file name, with underscores and no ext.
if "." in folder_name:
folder_name = folder_name[:folder_name.find(".")]
pathlib.Path(notebook_dir / folder_name).mkdir()
def create_note(file_path, notebook_dir):
'''
file_path : the full path of the file to be moved to Zim
'''
note_heading = ('Content-Type: text/x-zim-wiki\n'
+ 'Wiki-Format: zim 0.4\n\n'
+ '====== {} ======\n'
+ '{}\n\n')
link_text = 'Source:\n[[./{}|{}]]\n'
file_name = pathlib.Path(file_path).name
# note_title : file_name without its extensions
note_title = file_name[:file_name.find(".")]
# note folder : note_title with underscores
note_folder = note_title.replace(" ","_")
note_file = note_folder + ".txt"
note_link = link_text.format(file_name, note_title)
with open(pathlib.Path(notebook_dir) / note_file,'w',encoding='utf-8') as file:
file.write(note_heading.format(note_title, time.strftime("%Y-%m-%d")) + note_link)
# create the folder for the file
file_destination = pathlib.Path(notebook_dir / note_folder)
file_destination.mkdir()
shutil.copy(file_path, file_destination)
os.remove(file_path)
print(f"Created note for file: \n{file_path}\n")
def create_notes(input_dir, notebook_dir):
'''
Create Zim notes from a set of PDF files
'''
for file in os.listdir(input_dir):
input_file = pathlib.Path(input_dir / file)
create_note(input_file, notebook_dir)
# Set the input and Zim notebook directories
input_dir = # insert file path here
notebook_dir = pathlib.Path(# insert path to Zim notebook here)
# Send all the PDFs in the input directory to Zim.
create_notes(input_dir, notebook_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment