【问题标题】:Python convert txt file to pdfPython将txt文件转换为pdf
【发布时间】:2017-09-13 12:53:56
【问题描述】:

我想知道将路径中的 txt 文件批量转换为 PDF 的最简单方法是什么?

我在 Python https://github.com/baruchel/txt2pdf 中对此进行了研究 但我似乎无法在导入后在终端中调用 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

【问题讨论】:

  • 你的text2pdf.py在哪里?

标签: python pdf text converter


【解决方案1】:

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

如果你在终端,你可以像下面这样运行:

python txt2pdf.py yourfile.txt 
## Make sure the txt2pdf.py is at your working environment and also the .txt too

或者如果你在 jupyter notebook 中运行,只需运行如下:

run txt2pdf.py yourfile.txt

谢谢。

【讨论】:

    【解决方案2】:

    去这里看看click this

    这是您安装 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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多