Obsidian export to pdf via Typora

TLDR: This is a workaround to export pdf from Obsidian via typora using python script.

Requirement: You need to have python and Typora installed. And change two paths in the script as instructed.(I’m a python beginner, and I learnt regex few hours ago, so my code might be ugly… :joy:)

import re

attachment_folder = r"D:\\Important\\Obsidian\\My notes\\_resources"
file_path = "C:\\Users\\Frank\\Desktop\\test.md"

'''
replace above two paths with yours. 

Windows User:BACKSLASH NEEDS TO BE DOUBLED!!! No need to change code below.

Linux and MacOS user: use single slash '/' instead of double backslash '\\' in your two paths and change line 20's '\\' to '/' 

'''

with open(file_path, 'r+', encoding='utf-8') as md:

    orig_text = md.read()
    text = re.sub("!\[\[((.*)\.\w+)]]", r"![\2](" + attachment_folder + r"\\" +r"\1)", orig_text)

with open(file_path, 'w+', encoding='utf-8') as md:
    md.write(text)
2 Likes

If one has Typora installed, what is the advantage of using this script over opening the file in Typora and using Typora’s export option directly?

If you did not open markdown link format in Obsidian, link is [[pic.png]], this will not render correctly in typora. So you need to convert it to pic. This is what this script do for you.

The link you provide opens this:
oops

Oh, the format is interpreted. I mean image

Since Obsidian does not have native export method, I use typora to export.

@frankli998: got it.

Thank you…
Nice workaround!

My image links are ![](pic.png),
After I changed the setting of editor:

For the situation, add another code to modify ![](pic.png) to ![pic](attachpath/pic.png)

with open(file_path, 'r+', encoding='utf-8') as md:

    orig_text = md.read()
    text = re.sub("!\[\[((.*)\.\w+)\]\]", r"![\2](" + attachment_folder + r"\\" +r"\1)", orig_text)
    text = re.sub("!\[\]\(((.*)\.\w+)\)", r"![\2](" + attachment_folder + r"\\" +r"\1)", text)

And made some changes:

import re
import os

source_file_name =""
# source_file_name = "CNN"
source_folder = r"F:\\ObsidianVault\\Arsene\\AI\\Tensorflow" 
attachment_folder = rf'{source_folder}\\attach'
output_folder =  r"F:\\MDPrint"


def transfer_md(mdfile):   
    with open(f'{source_folder}\\{mdfile}.md', 'r+', encoding='utf-8') as md:
    
        orig_text = md.read()
        # let ![[pic.png]] ==> ![pic](<attach path>\pic.png)
        text = re.sub("!\[\[((.*)\.\w+)\]\]", r"![\2](" + attachment_folder + r"\\" +r"\1)", orig_text)
        # let ![](pic.png) ==> ![pic](<attach path>\pic.png)
        text = re.sub("!\[\]\(((.*)\.\w+)\)", r"![\2](" + attachment_folder + r"\\" +r"\1)", text)
        # let [[Link Name]] ==> Link Name,  [[Link Name|Altr Name]] ==> Altr Name
        text = re.sub("\[(?:\[|\[[^\]]*\|)([^\]\|]+)\]\]", r"\1", text)
    
    with open(f'{output_folder}\\{mdfile}.md', 'w+', encoding='utf-8') as md:
        md.write(text)
        
def trasfer_all_md(mddir):
    for file in [md[:-3] for md in os.listdir(mddir) if re.match('.*\.md',md)]:
        print(f'process file: {file}')
        transfer_md(file)

if __name__ == '__main__':
    if len(source_file_name)>0: # For one file
        transfer_md(source_file_name)
    else: # For entire folder
        trasfer_all_md(source_folder)

Sorry, but I’m not a programmer and have no clue how to do the task with Typora. Do I have to save the code in a .py file and need to run it as a command in Typora (added to the PDF export)?

Any support is appreciated :slightly_smiling_face:

And has anyone an idea how to convert the markdown file to a markdown file without the links? I tested this plugin, but I use internal links to headers in files and it didn’t work for that purpose.

I need the “clean” markdown for a glossary, which I use in a latex file for a thesis.