mdtable2csv
Recently I was trying to find an easy way to convert the tables in md to excel, instead of just copy paste.
After some research, I found that there are so many converters on the internet, but none of them can convert markdown file to excel. So I guess I have to make one on my own. And I was just happen to be learning Python, so I decided to write a Python! I also put it in my GitHub repo.
After lots of search on the internet, I found that GitHub provides a Markdown API that can convert your markdown to html. So I can send my markdown file using json format to github, and it’ll return the html format to me.
After I got the html, I first find all the contents in the <table>...</table>
tags, extract the rows in the HTML table from <tr>
tag. Then get the Header cells <th>
which contains the header information and Standard cells <td>
which contains data
separate headers and data by delimiter ,
.
最近由於工作上為了一些文件,有可能需要將markdown上面的表格貼到Excel上,但是總覺得用人工不是很可靠,所以想先找找看網路上有沒有人寫好工具可以用。
但是找了半天都只有轉成markdown的,或是把markdown轉成html之類的,並沒有轉成excel可以開的檔案。
剛好最近對Python有點興趣,就想說用Python來做一個吧!我也把我寫好的程式放在我的GitHub repo裡面了。
由於並沒有直接把.md轉成.csv的工具,但是在找的過程中發現GitHub有提供一個 Markdown API 可以讓你把markdown的內容包在json裡面丟上去,他就會把它轉成html的形式,我發現滿適合的就拿來用了。
當拿到html之後,剩下的就是從裡面把 <table>...</table>
標籤裡面的東西找出來,把每一行 <tr>
的標題 <th>
跟內容 <td>
然後用 ,
把每個內容分開,輸出成.csv的格式就完成了!
Here are some sample results of my work :