zoukankan      html  css  js  c++  java
  • PDF转图片工具

    点击下载( 提取码:1ll1 

    软件功能基于mupdf,UI使用wxpython开发

    功能:

    支持pdf转图片,图片格式png

    支持批量转换

    使用:

    第一步,点击按钮添加文档到列表,或直接将待转换文档拖入列表

    第二步,选择输出目录

    第三部,点击转换

    核心代码:

    class Debug(object):
        DEBUG = True
    
        def TraceLog(self, log=None):
            if Debug:
                import traceback
                traceback.print_exc()
                if log:
                    print(log)
    
    
    class PDF2IMGProcess(Debug):
        def __init__(self, path, callback=None):
            try:
                self.pdf_doc = fitz.open(path)
                self.file_name = os.path.basename(path)
                self.pages = self.pdf_doc.pageCount
                self.callback = callback
                self._Running = True
            except:
                self.TraceLog()
    
        def _TransFile(self, output_path):
            if self.callback:
                self.callback({'file': self.file_name, 'status': '开始!'})
            if self.pages > 0:
                output_path = os.path.join(output_path, self.file_name)
                os.makedirs(output_path, exist_ok=True)
            for p in range(self.pages):
                if not self._Running:
                    break
                if self._TransPage(output_path, p):
                    if self.callback:
                        self.callback({'file': self.file_name, 'page': p, 'status': 'Done!'})
                else:
                    if self.callback:
                        self.callback({'file': self.file_name, 'page': p, 'status': 'Error!'})
                        break
            if self.callback:
                self.callback({'file': self.file_name, 'status': '完成!'})
    
        def _TransPage(self, output_path, page_no, scale=1.8):
            output_name = os.path.join(output_path, '{}_{}.png'.format(self.file_name, page_no))
            page = self.pdf_doc.loadPage(page_no)
            matrix = fitz.Matrix(scale, scale)
            try:
                pix = page.getPixmap(matrix=matrix)
                pix.writePNG(output_name)
                return True
            except:
                self.TraceLog()
                return False
    
        def Start(self, output_path):
            self._Running = True
            Thread(target=self._TransFile, args=(output_path,)).start()
    
        def Cancel(self):
            self._Running = False
    

      

     

  • 相关阅读:
    SourceTree 跳过登陆
    input type="file"获取文件名方法
    Chrome浏览器安装vue-devtools插件
    设置文本超出既定宽度隐藏,并显示省略号
    node安装express时找不到pakage.json文件;判断安装成功?
    NoSQL:redis缓存数据库
    NoSQL:Linux操作memcached
    Python:迭代器
    Python函数篇:装饰器
    Python面向对象篇之元类,附Django Model核心原理
  • 原文地址:https://www.cnblogs.com/applex007/p/10346219.html
Copyright © 2011-2022 走看看