【问题标题】:I couldn't add variables in the argument我无法在参数中添加变量
【发布时间】:2021-11-29 02:11:23
【问题描述】:

该程序用于通过命令行获取用户的参数来为 pdf 添加水印 like(python watermarker.py wtr.pdf single.pdf twist.pdf super.pdf)

错误:回溯(最后一次调用): 文件“D:\pythonsaves\AndreiNeagoi\PROJECTS\PdfManager\watermarker.py”,第 9 行,在 水印 = PyPDF2.PdfFileWriter(open(str(mark), 'rb')) TypeError: init() 接受 1 个位置参数,但给出了 2 个

代码 -

 import PyPDF2
import sys

inputs = sys.argv[2:]
mark = sys.argv[1]


watermark = PyPDF2.PdfFileWriter(open(mark, 'rb'))
marker = PyPDF2.PdfFileWriter()

def Marker(watermark, pdf_list):
    for j in range(template.getNumPages()):
        page = template.getPage(j)
        page.mergePage(watermark.getPage(0))
        marker.addPage(page)

        with open('watermaked_output.pdf', 'wb') as file:
            marker.write(file)


for i in range(inputs):
    template = PyPDF2.PdfFileWriter(open( inputs[i], 'rb'))

    Marker(watermark, template)

【问题讨论】:

标签: python variables pdf arguments watermark


【解决方案1】:

似乎PyPDF2.PdfFileWriter对象的__init__函数只接受一个参数,即self;也就是说,在您的代码中,您只能通过调用instance=PyPDF2.PdfFileWriter() 来构造PdfFileWriter 对象,然后使用函数addPagewrite 来构造和输出文档。

看看这里:https://pythonhosted.org/PyPDF2/PdfFileWriter.html

【讨论】:

  • 您得到的错误 ("TypeError: init() 需要 1 个位置参数,但给出了 2 个") 显然意味着 PdfFileWriter 的构造函数除了 self 之外没有其他参数.
猜你喜欢
  • 2018-08-23
  • 2013-04-29
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 2015-01-28
  • 2012-09-03
相关资源
最近更新 更多