怎么用VSCode为今日头条写文章?(vscode自己写的头文件)

怎么用VSCode为今日条头写文章?

答案和摘要:用VSCode写Markdown文档,让AI写一个把Markdown文档转换为Word文档的python程序。

问题的来源

在今日头条发表图文并茂的文章,要求使用Word文档格式。而我作为码农,VSCode是最顺手的编辑器。这就产生的一个问题:怎么用VSCode为今日头条写文章呢?

先申明一下,我

o 不想买Word,更不用说MS Office全套,
o 不想看WPS的广告,
o LibreOffice竟然也要58元人民币,
o 我不想盗版,
o 而Pages又不合以前用Office/WPS的习惯,

那么我选择用VSCode,并不是因为娇情吧。

即便有MS Office可用,但考虑到我的文章中会插入很多代码,MS Office也不是一个好的选择啊。

对于我的写作来说,用Markdown格式写文章是最方便的,而且我的Word中过分灵活的格式变化,也是深恶痛绝。

所以,我真的有一个问题,怎么用VSCode的Markdown格式为今日头条写的Word格式的文章呢?

失败的方案一,利用VSCode插件

首先,我尝试了利用VSCode插件来完成这个任务,下载了一个Markdown转Word的插件,但是失败了。转换的效果很不好,会有一些莫明其妙的问题,比如标题字体有大有小,字体不存在等等;会有一些画蛇添足的问题,比如每页加上文档标题。

可能的方案二,利用写Markdown的软件

我尝试过一些免费的写Markdown的软件,没有一个比得上VSCode。而且考虑到我要插入代码到文章中,用两个编辑器写一篇文章,也不合理啊。

当然,我没有尝试过收费的软件。所以,这个方案暂时也不可行。

够用的方案三,用python程序

我最终选择了用python程序来完成这个任务。我手工撸过一个python程序来把Makrdown转换为Docx,但我限制了一些功能,比如不使用列表。因为处理Markdown文档,我是硬编码实现的,现在我想增加这些功能。

我想告诉朋友们,怎么写这个python程序呢?

我找了一个流行的AI(实际豆包,Deepseek等等都可以),一般来说我都是问下面几个问题:
1. 能给我写一个python程序把Markdown转换为Word吗?
2. 能加上图吗?
3. 图是本地的,读本地文件就可以,不需要访问网络。
4. 能加上ordered和unordered list吗?
5. 能加上code block吗?

然后,它就给出了一个python程序。

import markdown
from docx import Document
from docx.shared import Pt
from bs4 import BeautifulSoup
def markdown_to_word(md_file_path, word_file_path):
    # Read the Markdown file
    with open(md_file_path, "r", encoding="utf-8") as md_file:
        md_content = md_file.read()
    # Convert Markdown to HTML
    html_content = markdown.markdown(md_content, extensions=["fenced_code"])
    # Create a new Word document
    doc = Document()
    # Add custom style for code blocks
    styles = doc.styles
    if "Code" not in styles:
        style = styles.add_style("Code", 1)  # 1 means paragraph style
        font = style.font
        font.name = "Courier New"
        font.size = Pt(10)
    # Parse the HTML and add content to the Word document
    soup = BeautifulSoup(html_content, "html.parser")
    for element in soup.find_all(True):  # True means all tags
        if element.name == "h1":
            doc.add_heading(element.text.strip(), level=1)
        elif element.name == "h2":
            doc.add_heading(element.text.strip(), level=2)
        elif element.name == "h3":
            doc.add_heading(element.text.strip(), level=3)
        elif element.name == "p":
            doc.add_paragraph(element.text.strip())
        elif element.name == "ul":
            ul_list = []
            for li in element.find_all("li"):
                ul_list.append(li.text.strip())
            p = doc.add_paragraph()
            for item in ul_list:
                p.add_run(f"o {item}\n")
        elif element.name == "ol":
            ol_list = []
            for li in element.find_all("li"):
                ol_list.append(li.text.strip())
            p = doc.add_paragraph()
            for i, item in enumerate(ol_list, start=1):
                p.add_run(f"{i}. {item}\n")
        elif element.name == "a":
            run = doc.add_paragraph().add_run(element.text.strip())
            run.bold = True
            run.italic = True
        elif element.name == "strong":
            run = doc.add_paragraph().add_run(element.text.strip())
            run.bold = True
        elif element.name == "em":
            run = doc.add_paragraph().add_run(element.text.strip())
            run.italic = True
        elif element.name == "pre":
            code_block = element.code.string
            p = doc.add_paragraph(style="Code")
            p.add_run(code_block)
    # Save the Word document
    doc.save(word_file_path)
# Example usage
md_file = "writingForHeadline.md"
docx_file = "writingForHeadline.docx"
markdown_to_word(md_file, docx_file)
原文链接:,转发请注明来源!