python - 在 Python 中将文本文件转换为 PDF

标签 python

我有一个脚本,可以写入文本文件并将其放置在我的本地驱动器上。 python 是否有任何库或脚本可以将该文本文件转换为 PDF?

我正在使用下面的报告实验室,但出现错误:AttributeError:模块“reportlab.pdfgen.canvas”没有属性“drawString”

ptr = open(out_file, "r")  # text file I need to convert
    lineas = ptr.readlines()
    ptr.close()
    i = 750
    numeroLinea = 0

    while numeroLinea < len(lineas):
        if numeroLinea - len(lineas) < 60:  # I'm gonna write every 60 lines because I need it like that
            i = 750
            for linea in lineas[numeroLinea:numeroLinea + 60]:
                canvas.drawString(15, i, linea.strip())
                numeroLinea += 1
                i -= 12
            canvas.showPage()
        else:
            i = 750
            for linea in lineas[numeroLinea:]:
                canvas.drawString(15, i, linea.strip())
                numeroLinea += 1
                i -= 12
            canvas.showPage()

最佳答案

我认为您正在寻找的是 PyPDF2 库:

关于python - 在 Python 中将文本文件转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58103150/

相关文章:

python - celery celerybeat 可以在运行时动态添加/删除任务吗?

python - PyQt - 如果它已经在运行,如何检测和关闭 UI?

python - 谷歌 OAuth 与 Django

python - 返回单元素 Numpy 数组的简洁方法

python - 将 JSON 读取到 pandas 数据框 - ValueError : Mixing dicts with non-Series may lead to ambiguous ordering

Python - tkinter Treeview 不更新整数变量

python - 两个非连续项目的组合

Python HackerRank Bonetrousle 代码超时

Python - 获取邮件的日期时间 - Gmail

python - 快速读取 10000 个二进制文件?