当我使用Python从markdown生成pdf文件时,我遇到了问题。我的目标是将我的文档转换为pdf。为此,我已经有一个shell命令,如下所示:
markdown <markdown filename>.md | htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain --charset utf-8 --format pdf14 - > <pdf filename>.pdf
要使用它,您需要安装markdown和htmldoc:
sudo apt-get update
sudo apt-get install markdown
sudo apt-get install htmldoc
所以现在我想自动化这一代。我想在3.6中使用python及其主库subprocess
,所以这里是代码:
import subprocess
import os
import sys
import getopt
import shutil
def list_markdown_file(path):
# this function list all markdown file
# param path = path to the target directory
list_of_file = []
for file in os.listdir(path):
if file.endswith(".md") and not file == 'README.md':
list_of_file.append(os.path.splitext(file)[0])
return list_of_file
def generate_pdf(path, list_file):
destination_dir = "pdf"
if os.path.isdir(os.path.join(path, destination_dir)):
shutil.rmtree(os.path.join(path, destination_dir))
os.mkdir(os.path.join(path, destination_dir))
for filename in list_file:
subprocess.run(["markdown", filename+".md", "|", "htmldoc", "--cont",
"--headfootsize", "8.0", "--linkcolor", "blue", "--linkstyle",
"plain", "--charset", "utf-8", "--format", "pdf14", "-", ">",
os.path.join(path, destination_dir, filename+".pdf")], encoding='utf-8', stdout=subprocess.PIPE)
def main(argv):
try:
opts, args = getopt.getopt(argv, "hp:", ["path"])
except getopt.GetoptError:
print('python generate_pdf.py -p <path_to_directory>')
sys.exit(2)
path_to_file = ''
for opt, arg in opts:
if opt in ('-h', '--help'):
print('python generate_pdf.py -p <path_to_directory>')
sys.exit()
elif opt in ("-p", "--path"):
path_to_file = arg
if not opts:
print('python generate_pdf.py -h to see how its works')
exit(2)
list_of_file = list_markdown_file(path=path_to_file)
generate_pdf(path=path_to_file, list_file=list_of_file)
if __name__ == '__main__':
main(sys.argv[1:])
问题出在这一部分:
for filename in list_file:
subprocess.run(["markdown", filename+".md", "|", "htmldoc", "--cont",
"--headfootsize", "8.0", "--linkcolor", "blue", "--linkstyle",
"plain", "--charset", "utf-8", "--format", "pdf14", "-", ">",
os.path.join(path, destination_dir, filename+".pdf")], encoding='utf-8', stdout=subprocess.PIPE)
当我这样做时,只运行markdown filename.md
的部分。这是为什么?我该怎么做才能解决这个问题?
没有subprocess
的shell=True
运行一个子进程。如果要运行完整的管道,则需要使用shell=True
或单独运行每个进程。
与shell=True
平分但没有吸引力:
for filename in list_file:
# Switch run([list, of, things]) to (run("string of things", shell=True)
subprocess.run("""markdown '{0}.md' |
htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain \\
--charset utf-8 --format pdf14 - >'{1}'""".format(
filename, os.path.join(path, destination_dir, filename+".pdf"),
shell=True)
也许稍微优雅一点
for filename in list_file:
with open(os.path.join(path, destination_dir, filename+".pdf")) as dest:
subprocess.run("""markdown '{0}.md' |
htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain \\
--charset utf-8 --format pdf14 -""".format(filename),
shell=True, stdout=dest, universal_newlines=True, check=True)
你也可以摆脱shell=True
并运行两个独立的过程;请参阅Replacing shell pipeline文档中的subprocess
。
为了明确这一点,subprocess.run(['foo', 'bar', '|' 'baz'])
使用foo
,bar
和|
参数运行程序baz
;不是两个进程,其中第二个是baz
,第二个的标准输入连接到第一个的标准输出,这是shell运行管道时的作用。
您可以使用名为Markdown2PDF
的Python模块将Markdwen文件转换为PDF文件,由sudo pip3 install Markdown2PDF
在Python 3中安装它。打开终端并像md2pdf <file_name>
一样写md2pdf test.md
以转换为pdf。