在生成完毕或出现错误后会删除新产生的文件
parent
5d7f8c0772
commit
245c56f335
39
main.py
39
main.py
|
|
@ -11,18 +11,33 @@ from docx2pdf import convert
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
def done():
|
||||||
|
print("正在移除多余生成文件...")
|
||||||
|
for gen in gened_list:
|
||||||
|
os.remove(gen)
|
||||||
|
print("移除完毕!")
|
||||||
|
input("按任意键退出...")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
def deal_file_loss(path, dir_name):
|
||||||
|
print(f" {dir_name} 文件夹中文件个数不符,请检查!")
|
||||||
|
done()
|
||||||
|
|
||||||
def convert_all(path = os.path.abspath('.')):
|
def convert_all(path = os.path.abspath('.')):
|
||||||
print("正在扫描并处理文件中...")
|
print("正在扫描并处理文件中...")
|
||||||
items = os.listdir(path)
|
items = os.listdir(path)
|
||||||
for item in items:
|
for item in items:
|
||||||
if os.path.isdir(item):
|
if os.path.isdir(item):
|
||||||
|
if len(os.listdir(path + '\\' + item)) < 3:
|
||||||
|
deal_file_loss(path, item)
|
||||||
result = convert_img(item, path + '\\' + item)
|
result = convert_img(item, path + '\\' + item)
|
||||||
if result == 1:
|
if result == 1:
|
||||||
print("PDF 转换失败!")
|
print("PDF 转换失败!")
|
||||||
return
|
done()
|
||||||
elif result == 2:
|
elif result == 2:
|
||||||
print("JPG/JPEG 转换失败!")
|
print("JPG/JPEG 转换失败!")
|
||||||
return
|
done()
|
||||||
print("转换完毕!")
|
print("转换完毕!")
|
||||||
|
|
||||||
"""传入绝对路径"""
|
"""传入绝对路径"""
|
||||||
|
|
@ -39,7 +54,9 @@ def convert_img(dir_name, path = os.path.abspath('.')):
|
||||||
elif file.endswith('.jpg') or file.endswith('.jpeg'):
|
elif file.endswith('.jpg') or file.endswith('.jpeg'):
|
||||||
img = Image.open(path + '\\' + file)
|
img = Image.open(path + '\\' + file)
|
||||||
dot_index = file.rfind('.')
|
dot_index = file.rfind('.')
|
||||||
img.save(path + '\\' + file[:dot_index] + ".png", "PNG")
|
img_path = path + '\\' + file[:dot_index] + ".png"
|
||||||
|
img.save(img_path, "PNG")
|
||||||
|
gened_list.append(img_path)
|
||||||
if len(pdf_list) > 1:
|
if len(pdf_list) > 1:
|
||||||
print(f"注意到文件夹 {dir_name} 中有多个 .pdf 文件:")
|
print(f"注意到文件夹 {dir_name} 中有多个 .pdf 文件:")
|
||||||
for i in range(len(pdf_list)): # 打印 pdf 文件和选项
|
for i in range(len(pdf_list)): # 打印 pdf 文件和选项
|
||||||
|
|
@ -55,6 +72,8 @@ def convert_img(dir_name, path = os.path.abspath('.')):
|
||||||
invoice_path = path + '\\' + "pdf" + pdf_list[invoice_int][:-4] + ".png"
|
invoice_path = path + '\\' + "pdf" + pdf_list[invoice_int][:-4] + ".png"
|
||||||
invoice_renamed = path + '\\' + 'Pdf' + pdf_list[invoice_int][:-4] + ".png"
|
invoice_renamed = path + '\\' + 'Pdf' + pdf_list[invoice_int][:-4] + ".png"
|
||||||
os.rename(invoice_path, invoice_renamed) # 将 "pdf" 标签变为 "Pdf"
|
os.rename(invoice_path, invoice_renamed) # 将 "pdf" 标签变为 "Pdf"
|
||||||
|
gened_list.remove(invoice_path)
|
||||||
|
gened_list.append(invoice_renamed)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("请选择正确的序号!")
|
print("请选择正确的序号!")
|
||||||
|
|
@ -78,8 +97,8 @@ def gen_filelist(path = os.path.abspath('.')):
|
||||||
else:
|
else:
|
||||||
dir_file.remove(file)
|
dir_file.remove(file)
|
||||||
if png_count != 3:
|
if png_count != 3:
|
||||||
print(f"在{dir}文件夹发现错误:文件个数不符")
|
print(f"在{dir}文件夹发现错误:png文件个数不符")
|
||||||
return
|
done()
|
||||||
print("创建完毕!将要加入文档的文件如下:")
|
print("创建完毕!将要加入文档的文件如下:")
|
||||||
for dir, dir_file in file_list.items():
|
for dir, dir_file in file_list.items():
|
||||||
print('- ' + dir)
|
print('- ' + dir)
|
||||||
|
|
@ -125,16 +144,22 @@ def pdf2img(pdf_path, pdf_name, zoom_x = 3, zoom_y = 3):
|
||||||
return 1
|
return 1
|
||||||
for page in doc: # 遍历页面
|
for page in doc: # 遍历页面
|
||||||
pix = page.get_pixmap(matrix=fitz.Matrix(zoom_x, zoom_y)) # 将页面渲染为图片
|
pix = page.get_pixmap(matrix=fitz.Matrix(zoom_x, zoom_y)) # 将页面渲染为图片
|
||||||
pix.save(pdf_path + '\\' + "pdf" + pdf_name[:-4] + ".png") # 将图像存储为PNG格式
|
png_path = pdf_path + '\\' + "pdf" + pdf_name[:-4] + ".png"
|
||||||
|
gened_list.append(png_path)
|
||||||
|
pix.save(png_path) # 将图像存储为PNG格式
|
||||||
doc.close() # 关闭文档
|
doc.close() # 关闭文档
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
gened_list = [] # 新创建的文件列表,在出错时或者生成完毕时进行删除
|
||||||
print("在使用该脚本前,请保证程序所在的文件夹中仅包含程序、发票文件夹")
|
print("在使用该脚本前,请保证程序所在的文件夹中仅包含程序、发票文件夹")
|
||||||
print("并且确保每个发票文件夹内只有三个文件:发票为.pdf文件,其他为图片")
|
print("并且确保每个发票文件夹内只有三个文件")
|
||||||
input("按回车键开始...")
|
input("按回车键开始...")
|
||||||
convert_all()
|
convert_all()
|
||||||
gen_docx()
|
gen_docx()
|
||||||
print("生成 .pdf 文件中...")
|
print("生成 .pdf 文件中...")
|
||||||
convert("output.docx", "output.pdf")
|
convert("output.docx", "output.pdf")
|
||||||
print("生成完毕!")
|
print("生成完毕!")
|
||||||
|
done()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue