读取“最终”的目录下的png转换到“最终_pdf”目录下的pdf文件
import os
import img2pdf
# 读取“最终”的目录下的png转换到“最终_pdf”目录下的pdf文件
def png_to_pdf(file,pdf_name):
# 创建一个PDF文件 并以二进制方式写入
with open(pdf_name, "wb") as f:
# convert函数 用来转PDF
write_content = img2pdf.convert(file)
f.write(write_content) # 写入文件
directory = "./最终/"
# for root, dirs, files in os.walk(directory):
# for file in files:
# filelll = os.path.join(root, file)
# # print(file)
# pdf_name = filelll+file.replace(".png",".pdf")
# png_to_pdf(file,pdf_name)
for root, dirs, files in os.walk(directory):
for file in files:
# if ("工程材料、构配件、设备报审表" in file) and (".docx" in file):
filelll = os.path.join(root, file)
print(filelll)
pdf_name = filelll.replace("png","pdf")
pdf_name = pdf_name.replace("最终","最终_pdf")
print(pdf_name)
png_to_pdf(filelll,pdf_name)