diff --git a/README.md b/README.md index 003b14d..c552ca7 100644 --- a/README.md +++ b/README.md @@ -176,13 +176,16 @@ https://pandoc.org/installing.html 3. 路径格式 -全部使用绝对路径,类似:`C:\Users\kevin\Downloads\test.txt`。 +全部使用绝对路径,类似:`C:\Users\kevin\Downloads\test.txt`, 注意不要用引号包裹路径。 -Windows下可以选中目标文件后使用`Ctrl+Shift+C`快速复制文件绝对路径。 +> 如何快速获取文件绝对路径? +> 1. Windows下可以选中目标文件后使用`Ctrl+Shift+C`快速复制文件绝对路径。 +> 2. MacOS下可以选中目标文件后使用`Command+Opion+C`快速复制文件绝对路径。 软件会自动检测路径是否存在,不合法的路径将不会被通过,也不会进行继续的处理。 -如果想批量操作,可以使用通配符`*`。例如批量对PDF文件进行旋转,路径可以填`C:\Users\kevin\Downloads\*.pdf`,将会匹配`C:\Users\kevin\Downloads`目录下所有的PDF文件。除少数功能(插入/替换等)外,大部分都支持批量操作。 +如果想批量操作,可以使用通配符`*`。 +例如批量对PDF文件进行旋转,路径可以填`C:\Users\kevin\Downloads\*.pdf`,将会匹配`C:\Users\kevin\Downloads`目录下所有的PDF文件。除少数功能(插入/替换等)外,大部分都支持批量操作。 4. 坐标 diff --git a/thirdparty/pdf.py b/thirdparty/pdf.py index ed5fed3..85c8b89 100644 --- a/thirdparty/pdf.py +++ b/thirdparty/pdf.py @@ -1115,7 +1115,7 @@ def create_text_wartmark( y_offset : Union[int, float] = 0, multiple_mode : bool = False, output_path : str = None, - ) -> None: + ): try: if output_path is None: output_path = "watermark.pdf" @@ -1155,9 +1155,11 @@ def create_text_wartmark( c.drawString(start_x,start_y-i*line_height,part) c.save() dump_json(cmd_output_path, {"status": "success", "message": ""}) + return True except: logger.error(traceback.format_exc()) dump_json(cmd_output_path, {"status": "error", "message": traceback.format_exc()}) + return False def create_image_wartmark( width : Union[int, float], @@ -1202,9 +1204,11 @@ def create_image_wartmark( c.showPage() c.save() dump_json(cmd_output_path, {"status": "success", "message": ""}) + return True except: logger.error(traceback.format_exc()) dump_json(cmd_output_path, {"status": "error", "message": traceback.format_exc()}) + return False @batch_process def watermark_pdf_by_text(doc_path: str, wm_text: str, page_range: str = "all", layer: str = "bottom", output_path: str = None, **args): @@ -1213,7 +1217,9 @@ def watermark_pdf_by_text(doc_path: str, wm_text: str, page_range: str = "all", page = doc[-1] p = Path(doc_path) tmp_wm_path = str(p.parent / "tmp_wm.pdf") - create_text_wartmark(wm_text=wm_text, width=page.rect.width, height=page.rect.height, output_path=tmp_wm_path, **args) + ok = create_text_wartmark(wm_text=wm_text, width=page.rect.width, height=page.rect.height, output_path=tmp_wm_path, **args) + if not ok: + return wm_doc: fitz.Document = fitz.open(tmp_wm_path) roi_indices = parse_range(page_range, doc.page_count) for page_index in range(doc.page_count): @@ -1240,7 +1246,9 @@ def watermark_pdf_by_image(doc_path: str, wm_path: str, page_range: str = "all", page = doc[-1] p = Path(doc_path) tmp_wm_path = str(p.parent / "tmp_wm.pdf") - create_image_wartmark(wm_image_path=wm_path, width=page.rect.width, height=page.rect.height, output_path=tmp_wm_path, **args) + ok = create_image_wartmark(wm_image_path=wm_path, width=page.rect.width, height=page.rect.height, output_path=tmp_wm_path, **args) + if not ok: + return wm_doc = fitz.open(tmp_wm_path) roi_indices = parse_range(page_range, doc.page_count) for i in roi_indices: