def pic2pdf(pdf_file, pic_path): """ 把图片插入 pdf :param pdf_file:要保存的pdf文件 :param pic_path:图片目录 :return: """ t0 = time.perf_counter() # 生成图片初始时间 ll = sorted(glob.glob(f"{pic_path}/*")) Utils.sort_nicely(ll) # ll = ll[0:4] doc = fitz.open() for img in ll: # 读取图片,确保按文件名排序 print(img) imgdoc = fitz.open(img) # 打开图片 pdfbytes = imgdoc.convertToPDF() # 使用图片创建单页的 PDF imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) # 将当前页插入文档 if os.path.exists(pdf_file): os.remove(pdf_file) doc.save(pdf_file) # 保存pdf文件 doc.close() t1 = time.perf_counter() # 图片完成时间 print("总共合并了{}张图片".format(len(ll))) print("运行时间:{}s".format(t1 - t0))
def slot_toolbar_clicked(self, name): if name == 'doc转换': wg = self.canvas.currentWidget() # path = QtWidgets.QFileDialog.getExistingDirectory( # self, "选取源文件夹", r'F:\重要\法律与工程经济讲义', # QtWidgets.QFileDialog.ShowDirsOnly) # 起始路径 path = r'F:\重要\法律与工程经济讲义' print(path) # return if path: self.src_path = path self.src_files = Utils.files_in_dir(path, ['.doc', '.docx']) Utils.sort_nicely(self.src_files) print(self.src_files) if not self.src_files: return for each in self.src_files: item = QtWidgets.QListWidgetItem(QtGui.QIcon(), each, wg) # item.setToolTip(self.data[i]) item.setSizeHint(QtCore.QSize( 16777215, 50)) # 设置item的默认宽高(这里只有高度比较有用) item.setTextAlignment(QtCore.Qt.AlignCenter) # 文字居中 name, _ = os.path.splitext(each) doc = f'{path}\\{each}' pdf = f'{path}\\{name}.pdf' print(doc, pdf) self.butler.word2pdf(doc, pdf) # # # print(path) self.butler.merge_pdf(path, '合并.pdf') elif name == '缩图': file_list, _ = QtWidgets.QFileDialog.getOpenFileNames( self, '多文件选取', '', 'img Files(*.png *.jpg *.jpeg *.gif *.bmp *.tiff);;All Files(*.*)', None, QtWidgets.QFileDialog.DontUseNativeDialog) print(file_list) scale = 1.0 for each in file_list: file_in, ext = os.path.splitext(each) file_jpg = f'{file_in}.jpg' print(file_in, file_jpg, ext) ImageConvert.png_jpg(each, file_jpg, scale) elif name == '水印': # self.child_win.show() # ----- modeless dialog self.child_win.exec() # ------ modal dialog
def pic_pdf(pdf_name, pic_path): file_list = os.listdir(pic_path) pic_name = [] im_list = [] for x in file_list: if "jpg" in x or 'png' in x or 'jpeg' in x: pic_name.append(x) pic_name.sort() new_pic = [] for x in pic_name: if "jpg" in x: new_pic.append(x) for x in pic_name: if "png" in x: new_pic.append(x) Utils.sort_nicely(new_pic) # new_pic = new_pic[0:4] print("hec", len(new_pic), new_pic) im1 = Image.open(os.path.join(pic_path, new_pic[0])) # 第一幅图像文件 w, h = im1.size scale = 4 im1 = im1.resize((w // scale, h // scale), Image.ANTIALIAS) print(w, h, im1.size) new_pic.pop(0) for i in new_pic: img = Image.open(os.path.join(pic_path, i)) w, h = img.size img = img.resize((w // scale, h // scale), Image.ANTIALIAS) # print(type(img)) # im_list.append(Image.open(i)) if img.mode == "RGBA": img = img.convert('RGB') im_list.append(img) else: im_list.append(img) # out = im1.resize((w_new, h_new), Image.ANTIALIAS) im1.save(pdf_name, "PDF", resolution=100.0, save_all=True, append_images=im_list) print("输出文件名称:", pdf_name)
def flushDir(self): dir_path = self.data_path['dir_src'] if not dir_path: AnimWin('没有源目录') return tmp = Utils.getSubStr(dir_path, 10) # print(tmp) self.label_src.setText(u'源文件目录:' + tmp) self.files_all.clear() self.listWidget_src.clear() self.checked = False self.files_all = Utils.files_in_dir(dir_path, ['.doc', '.docx']) if not self.files_all: return Utils.sort_nicely(self.files_all) for each in self.files_all: item = QtWidgets.QListWidgetItem(each) self.listWidget_src.addItem(item) self.statusbar.showMessage(f'总文件:{len(self.files_all)} 选中:0', 0)