【问题标题】:Can I add line numbers to the left margin using pyfpdf我可以使用 pyfpdf 在左边距添加行号吗
【发布时间】:2019-06-14 05:37:57
【问题描述】:

如何使用 pyfpdf 或任何其他 pdf 创建库将连续行号添加到左边距?我想要的是类似于 MS Word 文档,左边距有行号,每一行都有编号。

【问题讨论】:

  • 在此处提问之前,请先搜索在线文档。并发布您尝试过的代码,即使它不完整。请参阅此pyfpdf doc 了解行号代码。

标签: python pdf pdf-generation python-pdfkit


【解决方案1】:

我尝试使用 PyPDF2 将行号添加到左边距。

# Importing module
import PyPDF2

# Creating object for pdf
pdfFileObj = open('/home/proton/Desktop/py.pdf','rb')

# Opening pdf
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

# Reading and printing total number of pages in the pdf
print("Number of pages:-"+str(pdfReader.numPages))
num = pdfReader.numPages

# Now I will read every page and all the lines of that page in pdf
i =0
while(i<num):
    line_number = 1
    pageObj = pdfReader.getPage(i)
    page_text=pageObj.extractText().strip().split('\n')
    for page_line in page_text:
        print(line_number,page_line)
        line_number+=1
    i= i+1

上面的代码打印了 pdf 中的所有行以及左侧的行号。如果您正在寻找任何可以在 pdf 中添加行号的内置函数,那么答案是“否”。您必须手动添加它。

如果你想写一个带有行号的新 pdf,你可以使用 PyPDF2.PdfFilewriter() ,使用 PyPDF2 的这个函数你可以写一个带有行号的新 pdf 文件 希望这会有所帮助:)

在此,我只打印了一页中带有行号的行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2017-03-16
    • 2016-03-12
    • 2011-08-06
    • 2020-09-02
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多