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

营销型网站建设的关键特点北京互联网营销公司

营销型网站建设的关键特点,北京互联网营销公司,c#网站开发视频教程 高清,大型搬家门户网站源码使用PyQt5应用程序制作PDF转换成图片的小工具,可以导入PDF文档后一键生成对应的PNG图片。 PDF图片转换小工具使用的中间件: python版本:3.6.8 UI应用版本:PyQt5 PDF文件操作非标准库:PyPDF2 PNG图片生成库&#xff1…

使用PyQt5应用程序制作PDF转换成图片的小工具,可以导入PDF文档后一键生成对应的PNG图片。

PDF图片转换小工具使用的中间件:

python版本:3.6.8
UI应用版本:PyQt5
PDF文件操作非标准库:PyPDF2
PNG图片生成库:PyMuPDF

pip install PyQt5pip install PyPDF2pip install PyMuPDF==1.18.17

将需要使用到的python标准库或非标准库全部导入到我们的代码块中进入开发环节。

# Importing all the classes from the PyQt5.QtGui module.
from PyQt5.QtGui import *# Importing all the classes from the PyQt5.QtCore module.
from PyQt5.QtCore import *# Importing all the classes from the PyQt5.QtWidgets module.
from PyQt5.QtWidgets import *# Importing the `fitz` module.
import fitz# Importing the PyPDF2 module.
import PyPDF2# Importing the `sys` module.
import sys# Importing the os module.
import os# Importing the traceback module.
import traceback

接下来直接进入正题,首先创建名称为PdfToPngUI的python类,将UI组件及布局和相关的槽函数都写入到这个类中。

# This class is a widget that contains a button and a text box. When the button is clicked, the text box is populated with
# the path to the converted file
class PdfToPngUI(QWidget):def __init__(self):"""A constructor. It is called when an object is created from a class and it allows the class to initialize theattributes of a class."""super(PdfToPngUI, self).__init__()self.init_ui()def init_ui(self):"""This function initializes the UI."""self.setWindowTitle('PDF图片转换工具 公众号:Python 集中营')self.setWindowIcon(QIcon('analysis.ico'))self.resize(600, 400)self.source_pdf_path = QLineEdit()self.source_pdf_path.setPlaceholderText('PDF文件路径')self.source_pdf_path.setReadOnly(True)self.source_pdf_btn = QPushButton()self.source_pdf_btn.setText('导入')self.source_pdf_btn.clicked.connect(self.source_pdf_btn_click)self.target_png_path = QLineEdit()self.target_png_path.setPlaceholderText('目标图片存储路径')self.target_png_path.setReadOnly(True)self.target_png_btn = QPushButton()self.target_png_btn.setText('路径')self.target_png_btn.clicked.connect(self.target_png_btn_click)self.start_btn = QPushButton()self.start_btn.setText('PDF一键生成PNG图片')self.start_btn.clicked.connect(self.start_btn_click)self.brower = QTextBrowser()self.brower.setReadOnly(True)self.brower.setFont(QFont('宋体', 8))self.brower.setPlaceholderText('日志处理过程区域...')self.brower.ensureCursorVisible()grid = QGridLayout()grid.addWidget(self.source_pdf_path, 0, 0, 1, 2)grid.addWidget(self.source_pdf_btn, 0, 2, 1, 1)grid.addWidget(self.target_png_path, 1, 0, 1, 2)grid.addWidget(self.target_png_btn, 1, 2, 1, 1)grid.addWidget(self.start_btn, 2, 0, 1, 3)grid.addWidget(self.brower, 3, 0, 1, 3)self.pdf_thread = WorkThread(self)self.pdf_thread.message.connect(self.show_message)self.pdf_thread.finished.connect(self.finished)self.setLayout(grid)def show_message(self, text):"""It shows a message:param text: The text to be displayed"""cursor = self.brower.textCursor()cursor.movePosition(QTextCursor.End)self.brower.append(text)self.brower.setTextCursor(cursor)self.brower.ensureCursorVisible()def source_pdf_btn_click(self):"""It opens a file dialog box to select the source PDF file."""source_pdf_path = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), "PDF File (*.pdf)")self.source_pdf_path.setText(source_pdf_path[0])def target_png_btn_click(self):"""A function that is called when the target_png_btn is clicked."""target_png_path = QFileDialog.getExistingDirectory(self, '选择文件夹', os.getcwd())self.target_png_path.setText(target_png_path)def start_btn_click(self):"""A function that is called when the start button is clicked."""self.pdf_thread.start()self.start_btn.setEnabled(False)def finished(self, finished):"""A function that is called when the target_png_btn is clicked"""if finished is True:self.start_btn.setEnabled(True)

通过上面的PdfToPngUI类处理,这个时候UI组件及布局和槽函数已经开发完成了,应用的页面效果如下。

PDF图片转换器UI页面.png

然后,我们开始业务逻辑的开发。这里将业务逻辑使用单独的子线程开发避免和页面的主线程发生阻塞。

创建一个子线程的python类WorkThread并继承自QThread子线程,将PDF图片转换的过程写到里面。

# It's a QThread that runs a function in a separate thread
class WorkThread(QThread):message = pyqtSignal(str)finished = pyqtSignal(bool)def __init__(self, parent=None):"""A constructor that initializes the class.:param parent: The parent widget"""super(WorkThread, self).__init__(parent)self.working = Trueself.parent = parentdef __del__(self):"""A destructor. It is called when the object is destroyed."""self.working = Falsedef run(self):"""PDF转换图片的业务函数。"""try:source_pdf_path = self.parent.source_pdf_path.text().strip()target_png_path = self.parent.target_png_path.text().strip()if source_pdf_path == '' or target_png_path == '':self.message.emit('来源文件路径或目标存储路径不能为空!')self.finished.emit(True)returnself.message.emit('源文件路径:{}'.format(source_pdf_path))self.message.emit('目标文件路径:{}'.format(target_png_path))pdf_ = fitz.open(source_pdf_path)self.message.emit('成功打开PDF文件对象!')reader = PyPDF2.PdfFileReader(source_pdf_path)self.message.emit('PDF文件流处理完成!')page_num = reader.getNumPages()self.message.emit('PDF文件页数读取完成!')for n in range(0, page_num):page = pdf_.load_page(n)pix_ = page.get_pixmap()pix_.save(os.path.join(target_png_path, str(n) + '.png'))self.message.emit('图片保存成功:{}'.format(os.path.join(target_png_path, str(n) + '.png')))self.message.emit('PNG图片全部转换完成!')self.finished.emit(True)except:traceback.print_exc()self.message.emit('程序运行出现错误,请检查参数是否设置正确!')self.finished.emit(True)

经过上述的UI界面组件以及业务线程的开发,功能已经实现了,下面使用main函数调起整个应用就OK了。

if __name__ == '__main__':app = QApplication(sys.argv)main = PdfToPngUI()main.show()sys.exit(app.exec_())

PDF图片转换.gif

往期精彩

假如有一个专属于python的终端工具,那绝对非他莫属!

如何解决python读取大数据量文件时造成的内存溢出?

python如何完成对 Excel文件的解密后读取?

http://www.bjxfkj.com.cn/article/100635.html

相关文章:

  • 企业网站带手机源码网络推广员为什么做不长
  • 龙岗网站建设 公司推广外贸网站推广方式
  • 做网站的草图 用什么画seo引擎搜索网站
  • 站点-将网站添加到区域变灰色无法添加如何解决哈尔滨最新消息
  • 公司网站怎么做包括什么深圳防疫措施优化
  • 关于做面包的网站seo外包软件
  • 做短视频网站用哪家cms舆情信息怎么写
  • 网站的栏目结构简图怎么做企业推广策划公司
  • 简历网站有哪些爱站网站长seo综合查询
  • 专业网站推广的公司网站seo排名优化工具在线
  • 做网站,好苦逼蜜雪冰城网络营销案例分析
  • 网站开发的技术风险公司主页网站设计
  • 学校网站模板大全百度一下你就知道了百度
  • 重庆建站模板厂家好用的搜索引擎有哪些
  • 做兼职去什么网站seo平台优化服务
  • 全屏网站制作湖人最新排名最新排名
  • 做网站市场价格常德网站设计
  • 北京市工程建设交易中心网站长沙百度首页优化排名
  • 网站建设外包协议新乡seo顾问
  • 广东的网站建设2022拉新推广赚钱的app
  • 黑河网站建设文案发布平台
  • 整个网站全部乱码网络平台营销
  • 衡阳市网站建设广州疫情最新新增
  • 怎样在网站图片上做店铺广告网络外贸推广
  • 日照蝶恋花网站建设互联网广告行业
  • 我在相亲网站做红娘的比较好网站制作公司
  • 赣州九一人才网最新招聘司机青岛seo服务
  • 外贸网站建设公司案例百度网站提交收录入口
  • 最好的书籍设计网站网站关键词优化工具
  • 永久网站web网页模板