forked from Dawn_Ocean/ZJUEVA-Reimburse
增加了一页仅两张图片生成文档的功能
parent
245c56f335
commit
7291dc1dbb
44
main.py
44
main.py
|
|
@ -9,35 +9,30 @@ import fitz
|
||||||
|
|
||||||
from docx2pdf import convert
|
from docx2pdf import convert
|
||||||
|
|
||||||
|
from sys import exit
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def done():
|
def done(exit_code = 0):
|
||||||
print("正在移除多余生成文件...")
|
print("正在移除多余生成文件...")
|
||||||
for gen in gened_list:
|
for gen in gened_list:
|
||||||
os.remove(gen)
|
os.remove(gen)
|
||||||
print("移除完毕!")
|
print("移除完毕!")
|
||||||
input("按任意键退出...")
|
input("按回车键退出...")
|
||||||
exit()
|
exit(exit_code)
|
||||||
|
|
||||||
|
|
||||||
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 转换失败!")
|
||||||
done()
|
done(1)
|
||||||
elif result == 2:
|
elif result == 2:
|
||||||
print("JPG/JPEG 转换失败!")
|
print("JPG/JPEG 转换失败!")
|
||||||
done()
|
done(1)
|
||||||
print("转换完毕!")
|
print("转换完毕!")
|
||||||
|
|
||||||
"""传入绝对路径"""
|
"""传入绝对路径"""
|
||||||
|
|
@ -89,16 +84,18 @@ def gen_filelist(path = os.path.abspath('.')):
|
||||||
for dir in dir_list:
|
for dir in dir_list:
|
||||||
file_list[dir] = os.listdir(path + '\\' + dir)
|
file_list[dir] = os.listdir(path + '\\' + dir)
|
||||||
for dir, dir_file in file_list.items():
|
for dir, dir_file in file_list.items():
|
||||||
png_count = 0
|
|
||||||
dir_file_copy = copy(dir_file)
|
dir_file_copy = copy(dir_file)
|
||||||
for file in dir_file_copy:
|
for file in dir_file_copy:
|
||||||
if ".png" in file.lower():
|
if ".png" not in file.lower():
|
||||||
png_count += 1
|
|
||||||
else:
|
|
||||||
dir_file.remove(file)
|
dir_file.remove(file)
|
||||||
if png_count != 3:
|
if len(dir_file) != 3:
|
||||||
print(f"在{dir}文件夹发现错误:png文件个数不符")
|
print(f"在{dir}文件夹发现错误:png文件个数不符")
|
||||||
done()
|
for file in dir_file:
|
||||||
|
print('- ' + file)
|
||||||
|
if len(dir_file) == 2: # 说明有两个文件
|
||||||
|
option = input("文件夹仅有两个文件,仍继续生成?(y/N)").lower()
|
||||||
|
if option != 'y':
|
||||||
|
done(1)
|
||||||
print("创建完毕!将要加入文档的文件如下:")
|
print("创建完毕!将要加入文档的文件如下:")
|
||||||
for dir, dir_file in file_list.items():
|
for dir, dir_file in file_list.items():
|
||||||
print('- ' + dir)
|
print('- ' + dir)
|
||||||
|
|
@ -110,7 +107,6 @@ def gen_filelist(path = os.path.abspath('.')):
|
||||||
return file_list
|
return file_list
|
||||||
|
|
||||||
def gen_docx(path = os.path.abspath('.')):
|
def gen_docx(path = os.path.abspath('.')):
|
||||||
while True:
|
|
||||||
doc = Document()
|
doc = Document()
|
||||||
file_list = gen_filelist()
|
file_list = gen_filelist()
|
||||||
input("按回车键确认...")
|
input("按回车键确认...")
|
||||||
|
|
@ -121,21 +117,21 @@ def gen_docx(path = os.path.abspath('.')):
|
||||||
for file in dir_file:
|
for file in dir_file:
|
||||||
if file[0:3] == "Pdf":
|
if file[0:3] == "Pdf":
|
||||||
dir_file.remove(file)
|
dir_file.remove(file)
|
||||||
dir_file.insert(0, file)
|
dir_file.insert(0, file) # 将发票文件放在第一个位置
|
||||||
doc.add_picture(parent_path + dir_file[0], height = Inches(2.5))
|
doc.add_picture(parent_path + dir_file[0], height = Inches(2.5))
|
||||||
table = doc.add_table(rows = 1, cols = 2)
|
table = doc.add_table(rows = 1, cols = 2)
|
||||||
cell1 = table.cell(0, 0)
|
cell1 = table.cell(0, 0)
|
||||||
cell1.paragraphs[0].add_run().add_picture(parent_path + dir_file[1], height = Inches(5.0))
|
cell1.paragraphs[0].add_run().add_picture(parent_path + dir_file[1], height = Inches(5.0))
|
||||||
cell2 = table.cell(0, 1)
|
cell2 = table.cell(0, 1)
|
||||||
|
if len(dir_file) == 3:
|
||||||
cell2.paragraphs[0].add_run().add_picture(parent_path + dir_file[2], height = Inches(5.0))
|
cell2.paragraphs[0].add_run().add_picture(parent_path + dir_file[2], height = Inches(5.0))
|
||||||
if (dir, dir_file) != list(file_list.items())[-1]: # dict.items() 返回值需先转换为列表,才能索引
|
if (dir, dir_file) != list(file_list.items())[-1]: # dict.items() 返回值需先转换为列表,才能索引
|
||||||
doc.add_page_break()
|
doc.add_page_break()
|
||||||
else:
|
else:
|
||||||
print("请检查文件命名是否正确!")
|
print("请检查文件结构是否正确!")
|
||||||
continue
|
done(1)
|
||||||
doc.save("output.docx")
|
doc.save("output.docx")
|
||||||
print("生成完毕!")
|
print("生成完毕!")
|
||||||
break
|
|
||||||
|
|
||||||
def pdf2img(pdf_path, pdf_name, zoom_x = 3, zoom_y = 3):
|
def pdf2img(pdf_path, pdf_name, zoom_x = 3, zoom_y = 3):
|
||||||
doc = fitz.open(pdf_path + "\\" + pdf_name) # 打开文档
|
doc = fitz.open(pdf_path + "\\" + pdf_name) # 打开文档
|
||||||
|
|
@ -159,7 +155,7 @@ if __name__ == "__main__":
|
||||||
print("生成 .pdf 文件中...")
|
print("生成 .pdf 文件中...")
|
||||||
convert("output.docx", "output.pdf")
|
convert("output.docx", "output.pdf")
|
||||||
print("生成完毕!")
|
print("生成完毕!")
|
||||||
done()
|
done(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue