运行脚本时我遇到了错误,该脚本应该获取图像的屏幕截图,然后将其转换为pdf文件。
import argparse
import os
import tempfile
import autopy
import img2pdf
def screenshot(top_left, right_bottom, next_page, total_page):
rect_size = (right_bottom[1153] - top_left[373], right_bottom[1167] - top_left[170])
images = []
temp_dir = tempfile.mkdtemp()
for i in range(total_page):
page_num = "{}".format(i).zfill(len(str(total_page)))
file_name = os.path.join(temp_dir, 'page-{}.png'.format(page_num))
images.append(file_name)
autopy.mouse.move(*next_page)
autopy.mouse.click(delay=1)
autopy.bitmap.capture_screen((top_left, rect_size)).save(file_name)
return images
def image2pdf(images):
with open("images.pdf", "wb") as f:
f.write(img2pdf.convert(images))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Take screenshots.')
parser.add_argument('top_left', type=str)
parser.add_argument('right_bottom', type=str)
parser.add_argument('next_button', type=str)
parser.add_argument('total_page', type=int)
args = parser.parse_args()
top_left = tuple(map(lambda x: int(x), args.top_left.split(',')))
right_bottom = tuple(map(lambda x: int(x), args.right_bottom.split(',')))
next_button = tuple(map(lambda x: int(x), args.next_button.split(',')))
total_page = args.total_page
print("Take screenshot at {} {} and next at {} with {} pages".format(
top_left, right_bottom, next_button, total_page
))
images = screenshot(top_left, right_bottom, next_button, total_page)
image2pdf(images)
print("Done, saved.")
这应该获取屏幕截图,然后转到下一页,继续全部拍摄,然后将所有图像转换为单个pdf文件。
这是我得到的错误
usage: app.py [-h] top_left right_bottom next_button total_page
app.py: error: the following arguments are required: top_left, right_bottom, next_button, total_page
答案 0 :(得分:0)
以下代码有效。参见下面我的运行方式。
python code.py 12,34 34,67 12,99 12
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Take screenshots.')
parser.add_argument('top_left', type=str)
parser.add_argument('right_bottom', type=str)
parser.add_argument('next_button', type=str)
parser.add_argument('total_page', type=int)
args = parser.parse_args()
top_left = tuple(map(lambda x: int(x), args.top_left.split(',')))
right_bottom = tuple(map(lambda x: int(x), args.right_bottom.split(',')))
next_button = tuple(map(lambda x: int(x), args.next_button.split(',')))
total_page = args.total_page
print("Take screenshot at {} {} and next at {} with {} pages".format(
top_left, right_bottom, next_button, total_page
))
print(args)
输出
Take screenshot at (12, 34) (34, 67) and next at (12, 99) with 12 pages
Namespace(next_button='12,99', right_bottom='34,67', top_left='12,34', total_page=12)