如何使用pyfpdf或任何其他pdf创建库将连续行号添加到左边距?我想要的是类似于MS Word文档的东西,在左边空白处有行号,其中每一行都有编号。
发布于 2019-06-14 14:53:59
我尝试过使用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中添加行号的内置函数,那么答案是"No“。您必须手动添加它。
如果你想用行号写一个新的pdf,你可以使用PyPDF2.PdfFilewriter(),使用PyPDF2的这个函数,你可以用行号写一个新的pdf文件希望这对你有帮助:)
在这个示例中,我只在一页中打印了一行行号

https://stackoverflow.com/questions/56591984
复制相似问题