当前位置: 首页 > news >正文

重庆网络公司网站建设中国刚刚发生的新闻

重庆网络公司网站建设,中国刚刚发生的新闻,环保网站建设公司哪家好,网站推广的基本方法为()最近同事经常找我帮她打印试卷,很奇葩的是,她的试卷都是*红书上下载的图片,一张张打印不好看,而且可能打印不完全,大小也不协调,所以有了这个脚本。 【需要给小孩打印图片格式的试卷时也比较实用】 -----…

最近同事经常找我帮她打印试卷,很奇葩的是,她的试卷都是*红书上下载的图片,一张张打印不好看,而且可能打印不完全,大小也不协调,所以有了这个脚本。

【需要给小孩打印图片格式的试卷时也比较实用】

---------------------------------------------------------------------------------------------------------------------------------

使用方法:


方法一:直接将脚本放到图片文件夹内,并运行脚本

方法二:命令行运行

命令行参数: python convert_images_to_pdf.py [<图片文件夹路径>] [<PDF保存路径>]


图片文件夹路径:可选,默认为:python脚本所在目录


PDF保存路径:可选,默认路径为 <图片文件夹路径> ,文件名为:<图片文件夹名>.PDF。需先指定<图片文件夹路径>再指定<PDF保存路径>

---------------------------------------------------------------------------------------------------------------------------------

命令行示例:


假设脚本名为:convert_images_to_pdf.py


只指定图片目录
> python convert_images_to_pdf.py C:\img_folder


同时指定图片目录和pdf路径
> python convert_images_to_pdf.py C:\img_folder  D:\result.pdf

代码:

# -*- coding: utf-8 -*-import os
import subprocess
import sysfrom PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvasdef pause_exit():subprocess.run("pause", shell=True)exit()def get_images(img_folder):"""遍历目录,获取目录下所有的图片"""img_format = (".jpg", ".png", ".bmp")images = []for file_name in os.listdir(img_folder):if file_name.lower().endswith(img_format):images.append(os.path.join(img_folder, file_name))return sorted(images)def get_image_size(img_file, page_width, page_height):"""设置每个图片的大小"""with Image.open(img_file) as img:img_width, img_height = img.sizeif img_height > page_height * 0.95 or img_width > page_width * 0.95:height_scale = (page_height * 0.95) / img_heightwidth_scale = (page_width * 0.95) / img_widthscale = min(height_scale, width_scale)img_width *= scaleimg_height *= scalereturn img_width, img_heightdef create_pdf(pdf_file, images, page_width, page_height):"""创建 pdf 文件,并添加图片"""c = canvas.Canvas(pdf_file, pagesize=letter)total_images = len(images)for i, img_path in enumerate(images):img_width, img_height = get_image_size(img_path, page_width, page_height)x = (page_width - img_width) / 2y = (page_height - img_height) / 2c.drawImage(img_path, x, y, img_width, img_height)c.showPage()progress_bar(i + 1, total_images)c.save()def create_pdf_from_path(img_folder, pdf_file=None):"""遍历给定路径,将路径下的图片添加进pdf,并将pdf保存在指定路径下"""images = get_images(img_folder)if images:if not pdf_file:pdf_name = os.path.basename(img_folder) + ".pdf"pdf_file = os.path.join(img_folder, pdf_name)page_width, page_height = lettercreate_pdf(pdf_file, images, page_width, page_height)return pdf_fileelse:print(f"{img_folder} 下没有图片,当前支持的图片格式为 jpg、png 和 bmp")pause_exit()def progress_bar(current, total, bar_length=60):"""进度条"""filled_length = int(bar_length * current // total)bar = "+" * filled_length + "-" * (bar_length - filled_length)percent = current / total * 100sys.stdout.write(f"\r处理进度:|{bar}| {percent:.2f}%")sys.stdout.flush()if __name__ == "__main__":subprocess.run("title 目录内图片转PDF", shell=True)try:(*rest,) = sys.argv[1:]if not rest:img_folder = os.getcwd()  # 使用当前文件夹作为 img_folderpdf_file = Noneelse:img_folder, *pdf_file = restpdf_file = pdf_file[0] if pdf_file else Noneexcept ValueError:print("请提供图片文件夹路径作为参数")pause_exit()if not os.path.exists(img_folder):print(f"{img_folder} 路径不存在!")pause_exit()elif not os.path.isdir(img_folder):print(f"{img_folder} 不是一个文件夹!")pause_exit()else:pdf_file = create_pdf_from_path(img_folder, pdf_file)if pdf_file:subprocess.run(["explorer", pdf_file])else:pause_exit()

http://www.dinnco.com/news/31502.html

相关文章:

  • 用户体验做的好的网站网络营销推广价格
  • 汽车网页设计论文郑州网站建设推广优化
  • 域名状态查询吉林seo刷关键词排名优化
  • 做热图的在线网站百度官方官网
  • 网站的专题怎么做国际军事最新头条新闻
  • tag做的最好的网站apple日本网站
  • 乐清网站艰涩自助建站系统
  • 江苏常州网站建设网址导航大全
  • 网站开发技术指标杭州最专业的seo公司
  • 电商做网站品牌整合推广
  • 直销网站建设关键词查网址
  • 北京做校园的网站单页关键词优化费用
  • 政府网站建设的目的怎么宣传网站
  • 德州做网站广州seo学徒
  • 安顺网站开发品牌设计公司
  • 公司企业免费网站系统找代写文章写手
  • 最近日本字幕mv免费高清在线惠州seo外包公司
  • 四川哪家网站推广做的好软文写作范文
  • 深圳做网站案例营销策划方案案例
  • 公司做网站的好处一个人怎么做独立站shopify
  • 做电商看的网站有哪些内容宁波seo网络推广推荐
  • 驻马店阿里巴巴做网站社群营销成功案例
  • 网站ico在后台哪里找到站长之家是干什么的
  • 中国做网站的网站惠州百度推广优化排名
  • 网站名称填写什么优化网站seo
  • wordpress 自定义主题成都网站seo设计
  • 潍坊专业网站建设网站设计模板网站
  • 佛山新网站制作代理商谷歌seo营销
  • joomla 网站建设三只松鼠营销策划书
  • 建设部建造师强制注销网站免费制作网站app