Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将txt文件转换为pdf_Python_Pdf_Text_Converter - Fatal编程技术网

Python将txt文件转换为pdf

Python将txt文件转换为pdf,python,pdf,text,converter,Python,Pdf,Text,Converter,我想知道什么是最简单的方式来转换txt文件的路径批转换为PDF 我已经在Python中对此进行了研究 但导入后,我似乎无法在终端中调用txt2pdf 还有其他建议吗 比如: text_file = open(filename, 'r') i = 0 for item in text_file: i += 1 f = open("c:\\workspace\\{0}.txt".format(i), 'w') txt2pdf convert (whatever goes he

我想知道什么是最简单的方式来转换txt文件的路径批转换为PDF

我已经在Python中对此进行了研究 但导入后,我似乎无法在终端中调用txt2pdf

还有其他建议吗

比如:

text_file = open(filename, 'r')
i = 0
for item in text_file:
    i += 1
    f = open("c:\\workspace\\{0}.txt".format(i), 'w')
    txt2pdf convert (whatever goes here)
        if i == 7:
           break
还尝试了使用ReportLab

def hello(c):
ic = 0
c = open("c:\\workspace\\simple\\{0}.txt".format(ic), 'w')
for item in c:
    ic += 1
    c = canvas.Canvas("c:\\workspace\\simple\\{0}.pdf".format(ic))
    hello(c)
    c.showPage()
    c.save()
    if ic == 7:
        break

不确定你是否已经找到了解决方案,只是在我搜索与你的问题相关的答案时碰巧看到了这个问题

如果您在终端,您可以按如下方式运行:

python txt2pdf.py yourfile.txt 
## Make sure the txt2pdf.py is at your working environment and also the .txt too
或者,如果您在jupyter笔记本中运行,只需按以下方式运行:

run txt2pdf.py yourfile.txt
谢谢。

到这里来看看怎么做

这是安装pdfkit后的代码,如上面链接的帖子所示

# from txt to html
# install wkthtml
import os
import pdfkit

with open("text.txt") as file:
    with open ("text.html", "w") as output:
        file = file.read()
        file = file.replace("\n", "<br>")
        output.write(file)

pdfkit.from_file("text.html", "output.pdf")

os.startfile("output.pdf")
#从txt到html
#安装WKTHL
导入操作系统
导入pdfkit
打开(“text.txt”)作为文件:
以open(“text.html”、“w”)作为输出:
file=file.read()
file=file.replace(“\n”和“
”) output.write(文件) pdfkit.from_文件(“text.html”、“output.pdf”) os.startfile(“output.pdf”)
您的
text2pdf.py
在哪里?