尝试写个批量把 markdown 转成 pdf 的脚本遇到问题

15次阅读

共计 2603 个字符,预计需要花费 7 分钟才能阅读完成。

自己学编程时做了很多笔记,如今累积起来已经有几十个了,有很多图片链,怕哪天图床垮了图片就找不到了,于是就想把当时的 md 文件都转成 pdf,看了点文章,就开始实践:

1. 下载 pandoc 和 CTex
2.chatGpt 弄一个脚本,需求是扫描脚本所在目录所有的 md 文件,并输出 pdf 文件到指定目录下,pdf 引擎用 xelatex, 转换用的 latex 模版需要和 md 文件目录放在一起,由于我用的 typora,想让输出的界面效果和 typora 的尽量一致,于是在 typora 官网上找到了个叫 eisvogel 的模版

脚本如下:

import os
import subprocess
import sys


directory_path = './'

# 确定输出文件夹
if len(sys.argv) == 2:
    output_folder = sys.argv[1]
else:
    output_folder = ""

# 确保输出文件夹存在
if output_folder and not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 获取目录下所有的 md 文件
md_files = [f for f in os.listdir(directory_path) if f.endswith(".md")]

for md_file in md_files:
    # 构建输出文件路径
    if output_folder:
        output_path = f'{output_folder}/{md_file.replace(".md",".pdf")}'
    else:
        output_path = f'{md_file.replace(".md",".pdf")}'

    # 构建 pandoc 命令
    cmd = f'pandoc"{md_file}"-o"{output_path}"--pdf-engine=xelatex --template eisvogel.latex --listings -V"CJKmainfont=‘Microsoft YaHei’"'
    print(cmd)
    os.system(cmd)

3. 执行脚本,结果每个 md 文件都报一堆看不懂的输出然后报错 Package fontspec Error,但是我检查过系统是有 Microsoft YaHei 这个字体的。

pandoc "C++ - 多重继承.md" -o "./pdf/C++ - 多重继承.pdf" --pdf-engine=xelatex --listings -V "CJKmainfont=‘Microsoft YaHei’"
This is METAFONT, Version 2.71828182 (MiKTeX 24.4)

(D:CTEXMiKTeXfonts/source/public/othelloOT.mf>> (0,0,line_thickness,0,0,line_thickness)
! Transform components aren't all known.

                   ;
set_black_circle->...circle.scaled.line_thickness;
                                                  lft.x1=1/22size;y1=1/2size...
l.39 set_black_circle
                     ;
>> 0.04347x2+0.52174
! Undefined x coordinate has been replaced by 0.

                   ..
set_black_circle->...size;y2=1/2size;filldraw.z1..
                                                  z2..cycle;black_circle:=cu...
l.39 set_black_circle
                     ;
>> 0.47826x2+0.23914
! Undefined y coordinate has been replaced by 0.

                   ..
set_black_circle->...size;y2=1/2size;filldraw.z1..
                                                  z2..cycle;black_circle:=cu...
l.39 set_black_circle
                     ;
>> x2
! Undefined x coordinate has been replaced by 0.

                   ..
set_black_circle->...;y2=1/2size;filldraw.z1..z2..
                                                  cycle;black_circle:=curren...
l.39 set_black_circle
                     ;
>> 0.47826x2+0.23914
! Undefined y coordinate has been replaced by 0.

                   ..
set_black_circle->...;y2=1/2size;filldraw.z1..z2..
                                                  cycle;black_circle:=curren...
l.39 set_black_circle
                     ;。。。。
                   ..
l.647   draw z5..
                 z6;
>> -0.5border_line_thickness
! Undefined y coordinate has been replaced by 0.

                   ;
l.647   draw z5..z6;

[18] )
(see the transcript file for additional information)
Font metrics written on OT.tfm.
Output written on OT.600gf (19 characters, 504 bytes).
Transcript written on OT.log.

Sorry, but miktex-maketfm did not succeed.

The log file hopefully contains the information to get MiKTeX going again:

  D:CTEXUserDatamiktexlogmiktex-maketfm.log
Error producing PDF.
! Package fontspec Error: 
(fontspec)                The font " 鈥楳 icrosoft YaHei 鈥?pandoc: : hPutChar: invalid argument (Invalid argument)

奇怪的是,把脚本输出的 cmd 字符串直接在 Powershell 上运行就能正常输出,于是我又把脚本改成只输出 cmd 文本,然后 CV 这些文本到 powershell 上批量运行,结果同样也有上述的错误,也就是说,可以单处理,就是不能批处理。

个人对 latex 的处理方式毫无经验,希望有懂的网友解释一下。

正文完
 0