如何使用 Adobe XI Pro 根据 PDF 中的短语删除页面?
How to delete pages based on phrases in PDF using Adobe XI Pro?
这是我第一次在 Adobe Pro 中使用 Actions。我愿意..
- 删除 PDF 中包含以下任一字符串的所有页面
(总计,Word 文档,Excel 电子表格)用于 Adobe Pro 中的 PDF。
- 从整个 PDF 的所有页面中删除常用字符串(CSI、导出、导入)。
以下代码是在网上找到的,地址为#1,但提取的页面基于 1 个字符串,我无法使其正常工作,我也希望 运行 通过多个字符串并删除页面.
// Iterates over all pages and find a given string and extracts all
// pages on which that string is found to a new file.
var pageArray = [];
var stringToSearchFor = "Total";
for (var p = 0; p < this.numPages; p++) {
// iterate over all words
for (var n = 0; n < this.getPageNumWords(p); n++) {
if (this.getPageNthWord(p, n) == stringToSearchFor) {
pageArray.push(p);
break;
}
}
}
if (pageArray.length > 0) {
// extract all pages that contain the string into a new document
var d = app.newDoc(); // this will add a blank page - we need to remove that once we are done
for (var n = 0; n < pageArray.length; n++) {
d.insertPages( {
nPage: d.numPages-1,
cPath: this.path,
nStart: pageArray[n],
nEnd: pageArray[n],
} );
}
// remove the first page
d.deletePages(0);
}
- 一个词和两个词的短语选项。
一个词:
for (var p=this.numPages-1; p>=0; p--) {
if (this.numPages==1) break;
for (var n=0; n<this.getPageNumWords(p)-1; n++) {
if (this.getPageNthWord(p, n) == "one-word") {
this.deletePages(p);
break;
}
}
}
两个词:
for (var p=this.numPages-1; p>=0; p--) {
if (this.numPages==1) break;
for (var n=0; n<this.getPageNumWords(p)-1; n++) {
if (this.getPageNthWord(p, n) == "1st-word" && this.getPageNthWord(p, n+1) == "2nd-word") {
this.deletePages(p);
break;
}
}
}
- 在 Adobe XI Pro 中,工具--> 保护--> 搜索并删除文本
我也遇到过类似的需求,当某个单词存在时从 PDF 中删除页面。我有 35000 个文档和 80000-230000 页。
运行 javascript 真的很慢。
我还尝试了 Autobookmark-plugin 来自 Evermap 的 Adobe Acrobat - 它可以处理几十页的文件,但 20000 页没有完成这个过程,插件可能 运行 在 80000 左右出现 RAM 问题页数。
所以,然后我查看了我之前知道的事情:
Powershell - PDF 编辑和处理模块似乎不存在,或者是旧的,或者...
Python - 成功了!我下面的代码(大部分是从网络上的其他人那里复制的!),我使用 Anaconda 包,它很容易设置。在 运行 脚本之前你必须安装一些模块并且代码可以使用一些整理:
# Import modules
import PyPDF2
import re
import pandas
# open the pdf file
object = PyPDF2.PdfFileReader("C:\folder\file.pdf") #python style path
# get number of pages
NumPages = object.getNumPages()
# define keyterm to search
String = "word"
ListPages = []
# extract text and do the search on each page
for i in range(0, NumPages):
PageObj = object.getPage(i)
Text = PageObj.extractText()
if re.search(String,Text):
ListPages.append(i)
# print("Pattern Found on Page: " + str(i))
# the pages to delete
pages_to_delete = ListPages
infile = PyPDF2.PdfFileReader("C:\folder\file.pdf", 'rb')
output = PyPDF2.PdfFileWriter()
for i in range(infile.getNumPages()):
if i not in pages_to_delete:
p = infile.getPage(i)
output.addPage(p)
with open("C:\folder\newfile.pdf", 'wb') as f:
output.write(f)
这是我第一次在 Adobe Pro 中使用 Actions。我愿意..
- 删除 PDF 中包含以下任一字符串的所有页面 (总计,Word 文档,Excel 电子表格)用于 Adobe Pro 中的 PDF。
- 从整个 PDF 的所有页面中删除常用字符串(CSI、导出、导入)。
以下代码是在网上找到的,地址为#1,但提取的页面基于 1 个字符串,我无法使其正常工作,我也希望 运行 通过多个字符串并删除页面.
// Iterates over all pages and find a given string and extracts all
// pages on which that string is found to a new file.
var pageArray = [];
var stringToSearchFor = "Total";
for (var p = 0; p < this.numPages; p++) {
// iterate over all words
for (var n = 0; n < this.getPageNumWords(p); n++) {
if (this.getPageNthWord(p, n) == stringToSearchFor) {
pageArray.push(p);
break;
}
}
}
if (pageArray.length > 0) {
// extract all pages that contain the string into a new document
var d = app.newDoc(); // this will add a blank page - we need to remove that once we are done
for (var n = 0; n < pageArray.length; n++) {
d.insertPages( {
nPage: d.numPages-1,
cPath: this.path,
nStart: pageArray[n],
nEnd: pageArray[n],
} );
}
// remove the first page
d.deletePages(0);
}
- 一个词和两个词的短语选项。
一个词:
for (var p=this.numPages-1; p>=0; p--) {
if (this.numPages==1) break;
for (var n=0; n<this.getPageNumWords(p)-1; n++) {
if (this.getPageNthWord(p, n) == "one-word") {
this.deletePages(p);
break;
}
}
}
两个词:
for (var p=this.numPages-1; p>=0; p--) {
if (this.numPages==1) break;
for (var n=0; n<this.getPageNumWords(p)-1; n++) {
if (this.getPageNthWord(p, n) == "1st-word" && this.getPageNthWord(p, n+1) == "2nd-word") {
this.deletePages(p);
break;
}
}
}
- 在 Adobe XI Pro 中,工具--> 保护--> 搜索并删除文本
我也遇到过类似的需求,当某个单词存在时从 PDF 中删除页面。我有 35000 个文档和 80000-230000 页。
运行 javascript 真的很慢。
我还尝试了 Autobookmark-plugin 来自 Evermap 的 Adobe Acrobat - 它可以处理几十页的文件,但 20000 页没有完成这个过程,插件可能 运行 在 80000 左右出现 RAM 问题页数。
所以,然后我查看了我之前知道的事情:
Powershell - PDF 编辑和处理模块似乎不存在,或者是旧的,或者...
Python - 成功了!我下面的代码(大部分是从网络上的其他人那里复制的!),我使用 Anaconda 包,它很容易设置。在 运行 脚本之前你必须安装一些模块并且代码可以使用一些整理:
# Import modules
import PyPDF2
import re
import pandas
# open the pdf file
object = PyPDF2.PdfFileReader("C:\folder\file.pdf") #python style path
# get number of pages
NumPages = object.getNumPages()
# define keyterm to search
String = "word"
ListPages = []
# extract text and do the search on each page
for i in range(0, NumPages):
PageObj = object.getPage(i)
Text = PageObj.extractText()
if re.search(String,Text):
ListPages.append(i)
# print("Pattern Found on Page: " + str(i))
# the pages to delete
pages_to_delete = ListPages
infile = PyPDF2.PdfFileReader("C:\folder\file.pdf", 'rb')
output = PyPDF2.PdfFileWriter()
for i in range(infile.getNumPages()):
if i not in pages_to_delete:
p = infile.getPage(i)
output.addPage(p)
with open("C:\folder\newfile.pdf", 'wb') as f:
output.write(f)