原博主网址如下,仅作学习记录。
https://blog.csdn.net/qq_42003943/article/details/124385592?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-8-124385592-blog-125025413.235v27pc_relevant_default&spm=1001.2101.3001.4242.5&utm_relevant_index=11
import os
from PyPDF2 import PdfFileWriter, PdfFileReader
def get_file(path, all_files):
FileNames = os.listdir(path)
for file_name in FileNames:
cur_path = os.path.join(path, file_name)
if os.path.isdir(cur_path):
get_file(cur_path, all_files)
else:
if file_name.endswith('.pdf'):
all_files.append(cur_path)
return all_files
def extract_one_pdf(pdf_path, save_dir, start_page, end_page):
pdf_name = pdf_path.split("\\")[-1]
print(f"Processing file -- {pdf_path}")
if not os.path.exists(save_dir):
print(save_dir)
os.makedirs(save_dir)
pdf_file = PdfFileReader(pdf_path, strict=False)
pdf_pages = pdf_file.getNumPages()
if not -1 < start_page < end_page < pdf_pages:
raise ValueError("Pages Error")
pdf_output = PdfFileWriter()
for i in range(start_page, end_page):
page = pdf_file.getPage(i)
pdf_output.addPage(page)
with open(save_dir + pdf_name, 'wb') as out:
pdf_output.write(out)
def extract_many_pdf(pdf_dir_path, save_path, start_page, end_page):
count=0
source_files = get_file(pdf_dir_path, [])
for i, file in enumerate(source_files):
try:
file_name = file.split("\\")[-1]
save_file = file.replace(pdf_dir_path, save_path)
save_dir = save_file.replace(file_name, '')
count = count +1
extract_one_pdf(file, save_dir, start_page, end_page)
except:
print("文件错误:",file_name)
continue
print("本次共计处理",count,"个文件")
print('process finished,please check')
if __name__ == '__main__':
path = "Z:\\00\\"
out_path = "Z:\\01\\"
extract_many_pdf(path, out_path, 0, 1)
import os
import xlwt #操作excel模块
import sys
path25= "Z:\\00\\"
file_path = path25+'\\清单识别数据.xls'#sys.path[0]为要获取当前路径,filenamelist为要写入的文件
f = xlwt.Workbook(encoding='utf-8', style_compression=0) #新建一个excel
sheet = f.add_sheet('sheet1') #新建一个sheet
pathDir = os.listdir(path25)#文件放置在当前文件夹中,用来获取当前文件夹内所有文件目录
i = 0 #将文件列表写入test.xls
for s in pathDir:
sheet.write(i, 0, s) #参数i,0,s分别代表行,列,写入值
i = i+1
print(file_path)
print(i) #显示文件名数量
f.save(file_path)
import os
import xlwt #操作excel模块
import sys
path25= "Z:\\01\\"
file_path = path25+'\\清单识别数据.xls'#sys.path[0]为要获取当前路径,filenamelist为要写入的文件
f = xlwt.Workbook(encoding='utf-8', style_compression=0) #新建一个excel
sheet = f.add_sheet('sheet1') #新建一个sheet
pathDir = os.listdir(path25)#文件放置在当前文件夹中,用来获取当前文件夹内所有文件目录
i = 0 #将文件列表写入test.xls
for s in pathDir:
sheet.write(i, 0, s) #参数i,0,s分别代表行,列,写入值
i = i+1
print(file_path)
print(i) #显示文件名数量
f.save(file_path)
https://www.cnblogs.com/weiyangoo/p/14248061.html
# -*- coding:utf-8*-
# 利用PyPDF2模块合并同一文件夹下的所有PDF文件
# 只需修改存放PDF文件的文件夹变量:file_dir 和 输出文件名变量: outfile
import os
from PyPDF2 import PdfFileReader, PdfFileWriter
import time
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
# 使用os模块的walk函数,搜索出指定目录下的全部PDF文件
# 获取同一目录下的所有PDF文件的绝对路径
def getFileName(filedir):
file_list = [os.path.join(root, filespath) \
for root, dirs, files in os.walk(filedir) \
for filespath in files \
if str(filespath).endswith('pdf')
]
return file_list if file_list else []
# 合并同一目录下的所有PDF文件
def MergePDF(filepath, outfile):
output = PdfFileWriter()
outputPages = 0
pdf_fileName = getFileName(filepath)
if pdf_fileName:
for pdf_file in pdf_fileName:
print("路径:%s"%pdf_file)
# 读取源PDF文件
input = PdfFileReader(open(pdf_file, "rb"),strict=False)
#获得源PDF文件中页面总数
pageCount = input.getNumPages()
outputPages += pageCount
print("页数:%d"%pageCount)
# 分别将page添加到输出output中
for iPage in range(pageCount):
output.addPage(input.getPage(iPage))
print("合并后的总页数:%d."%outputPages)
# 写入到目标PDF文件
outputStream = open(os.path.join(filepath, outfile), "wb")
output.write(outputStream)
outputStream.close()
print("PDF文件合并完成!")
else:
print("没有可以合并的PDF文件!")
主函数如下:
# 主函数
def main():
time1 = time.time()
file_dir = r"Z:\01" #待合并pdf路径
outfile = "merge.pdf" #合并好的pdf文件名
MergePDF(file_dir, outfile)
time2 = time.time()
print('总共耗时:%s s.' %(time2 - time1))
main()