小白作品不习勿喷 欢迎优化
from DrissionPage import Chromium
from PIL import Image
import time
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
url_img = input('请输入图片地址:')
# 创建一个Chromium实例
chromium = Chromium()
tab = chromium.latest_tab
tab.get(url_img)
delete_tool = tab.ele('#toolbar')
tab.remove_ele(delete_tool)
continueButton = tab.ele('#continueButton')
if continueButton:
continueButton.click()
# 获取元素并截图
element_name_list = []
page_elements = tab.eles('#^outer_page')
print(page_elements)
for img in page_elements:
element_id = img.attrs
element_name_list.append(element_id['id'] + '.png')
time.sleep(1)
element_name = element_id['id']
time.sleep(1)
img_png = tab.ele(f'#{element_name}')
time.sleep(3)
img_png.get_screenshot()
img.get_screenshot(path=None, name=f'{element_name}.png')
print(f'{element_name} 已保存。')
print(element_name_list)
pdf_filename = 'a.pdf'
c = canvas.Canvas(pdf_filename, pagesize=letter)
width, height = letter
for png_file in element_name_list:
# 打开 PNG 图片
img = Image.open(png_file)
# 获取图片的宽度和高度
img_width, img_height = img.size
# 按比例调整图片大小以适应 PDF 页面
ratio = min(width / img_width, height / img_height)
new_width = img_width * ratio
new_height = img_height * ratio
# 计算图片在 PDF 页面的位置
x = (width - new_width) / 2
y = (height - new_height) / 2
# 将图片添加到 PDF
c.drawImage(png_file, x, y, width=new_width, height=new_height)
# 如果还有更多图片,则创建新页面
if png_file != element_name_list[-1]:
c.showPage()
c.save()
https://wwbu.lanzouq.com/iGhrm2f87kle
实测
2ef0d7ccefd95bd8ed021da1b44dad5.png (30.52 KB, 下载次数: 0)
下载附件
1
2024-11-16 15:28 上传