forked from Dawn_Ocean/ZJUEVA-Reimburse
Compare commits
2 Commits
0d1fe48262
...
cc217751d1
| Author | SHA1 | Date |
|---|---|---|
|
|
cc217751d1 | |
|
|
b594ee010d |
|
|
@ -1 +1,5 @@
|
||||||
.venv
|
.venv
|
||||||
|
/test
|
||||||
|
build
|
||||||
|
dist
|
||||||
|
main.spec
|
||||||
59
main.py
59
main.py
|
|
@ -26,7 +26,7 @@ def convert_all(path = os.path.abspath('.')):
|
||||||
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):
|
||||||
result = convert_img(item, path + '\\' + item)
|
result = convert_img(item, path + os.sep + item)
|
||||||
if result == 1:
|
if result == 1:
|
||||||
print("PDF 转换失败!")
|
print("PDF 转换失败!")
|
||||||
done(1)
|
done(1)
|
||||||
|
|
@ -47,12 +47,27 @@ def convert_img(dir_name, path = os.path.abspath('.')):
|
||||||
return 1
|
return 1
|
||||||
pdf_list.append(file)
|
pdf_list.append(file)
|
||||||
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 + os.sep + file)
|
||||||
dot_index = file.rfind('.')
|
dot_index = file.rfind('.')
|
||||||
img_path = path + '\\' + file[:dot_index] + ".png"
|
img_path = path + os.sep + file[:dot_index] + ".png"
|
||||||
img.save(img_path, "PNG")
|
img.save(img_path, "PNG")
|
||||||
gened_list.append(img_path)
|
gened_list.append(img_path)
|
||||||
if len(pdf_list) > 1:
|
if len(pdf_list) > 1:
|
||||||
|
fp_name = ["发票","dzfp"]
|
||||||
|
finish = False
|
||||||
|
for i in fp_name:
|
||||||
|
for j in range(len(pdf_list)):
|
||||||
|
if i in pdf_list[j]:
|
||||||
|
invoice_path = path + os.sep + "pdf" + pdf_list[j][:-4] + ".png"
|
||||||
|
invoice_renamed = path + os.sep + 'Pdf' + pdf_list[j][:-4] + ".png"
|
||||||
|
os.rename(invoice_path, invoice_renamed) # 将 "pdf" 标签变为 "Pdf"
|
||||||
|
gened_list.remove(invoice_path)
|
||||||
|
gened_list.append(invoice_renamed)
|
||||||
|
finish = True
|
||||||
|
break
|
||||||
|
if finish == True:
|
||||||
|
break
|
||||||
|
if finish == False:
|
||||||
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 文件和选项
|
||||||
print(f"{i + 1} - {pdf_list[i]}") # 索引从 0 改为 1
|
print(f"{i + 1} - {pdf_list[i]}") # 索引从 0 改为 1
|
||||||
|
|
@ -64,8 +79,8 @@ def convert_img(dir_name, path = os.path.abspath('.')):
|
||||||
else:
|
else:
|
||||||
invoice_int = int(invoice_index) - 1 # 索引从 1 改为 0
|
invoice_int = int(invoice_index) - 1 # 索引从 1 改为 0
|
||||||
if invoice_int < len(pdf_list) and invoice_int >= 0:
|
if invoice_int < len(pdf_list) and invoice_int >= 0:
|
||||||
invoice_path = path + '\\' + "pdf" + pdf_list[invoice_int][:-4] + ".png"
|
invoice_path = path + os.sep + "pdf" + pdf_list[invoice_int][:-4] + ".png"
|
||||||
invoice_renamed = path + '\\' + 'Pdf' + pdf_list[invoice_int][:-4] + ".png"
|
invoice_renamed = path + os.sep + '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.remove(invoice_path)
|
||||||
gened_list.append(invoice_renamed)
|
gened_list.append(invoice_renamed)
|
||||||
|
|
@ -82,20 +97,16 @@ def gen_filelist(path = os.path.abspath('.')):
|
||||||
dir_list.append(item)
|
dir_list.append(item)
|
||||||
file_list = {}
|
file_list = {}
|
||||||
for dir in dir_list:
|
for dir in dir_list:
|
||||||
file_list[dir] = os.listdir(path + '\\' + dir)
|
file_list[dir] = os.listdir(path + os.sep + dir)
|
||||||
for dir, dir_file in file_list.items():
|
for dir, dir_file in file_list.items():
|
||||||
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" not in file.lower():
|
if ".png" not in file.lower():
|
||||||
dir_file.remove(file)
|
dir_file.remove(file)
|
||||||
if len(dir_file) != 3:
|
if len(dir_file) != 3 or len(dir_file) != 2:
|
||||||
print(f"在{dir}文件夹发现错误:png文件个数不符")
|
print(f"在{dir}文件夹发现错误:png文件个数不符")
|
||||||
for file in dir_file:
|
for file in dir_file:
|
||||||
print('- ' + 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)
|
||||||
|
|
@ -113,18 +124,25 @@ def gen_docx(path = os.path.abspath('.')):
|
||||||
print("生成 .docx 文档中...")
|
print("生成 .docx 文档中...")
|
||||||
if len(file_list) != 0:
|
if len(file_list) != 0:
|
||||||
for dir, dir_file in file_list.items():
|
for dir, dir_file in file_list.items():
|
||||||
parent_path = path + '\\' + dir + '\\'
|
parent_path = path + os.sep + dir + os.sep
|
||||||
|
for file in dir_file:
|
||||||
|
if file[0:3] == "pdf":
|
||||||
|
dir_file.remove(file)
|
||||||
|
dir_file.insert(0, file)
|
||||||
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(4.2))
|
||||||
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))
|
if "行程单" in dir_file[1]:
|
||||||
|
cell1.paragraphs[0].add_run().add_picture(parent_path + dir_file[1], height = Inches(4.0))
|
||||||
|
else:
|
||||||
|
cell1.paragraphs[0].add_run().add_picture(parent_path + dir_file[1], height = Inches(4.0))
|
||||||
cell2 = table.cell(0, 1)
|
cell2 = table.cell(0, 1)
|
||||||
if len(dir_file) == 3:
|
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(4.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:
|
||||||
|
|
@ -133,17 +151,22 @@ def gen_docx(path = os.path.abspath('.')):
|
||||||
doc.save("output.docx")
|
doc.save("output.docx")
|
||||||
print("生成完毕!")
|
print("生成完毕!")
|
||||||
|
|
||||||
def pdf2img(pdf_path, pdf_name, zoom_x = 3, zoom_y = 3):
|
def pdf2img(pdf_path, pdf_name, zoom_x = 7, zoom_y = 7):
|
||||||
doc = fitz.open(pdf_path + "\\" + pdf_name) # 打开文档
|
doc = fitz.open(pdf_path + os.sep + pdf_name) # 打开文档
|
||||||
if len(doc) != 1:
|
if len(doc) != 1:
|
||||||
print("PDF 文件只能包含一页!")
|
print("PDF 文件只能包含一页!")
|
||||||
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)) # 将页面渲染为图片
|
||||||
png_path = pdf_path + '\\' + "pdf" + pdf_name[:-4] + ".png"
|
png_path = pdf_path + os.sep + "pdf" + pdf_name[:-4] + ".png"
|
||||||
gened_list.append(png_path)
|
gened_list.append(png_path)
|
||||||
pix.save(png_path) # 将图像存储为PNG格式
|
pix.save(png_path) # 将图像存储为PNG格式
|
||||||
doc.close() # 关闭文档
|
doc.close() # 关闭文档
|
||||||
|
if "行程" in pdf_name:
|
||||||
|
img = Image.open(pdf_path + os.sep + "pdf" + pdf_name[:-4] + ".png")
|
||||||
|
if img.height > img.width:
|
||||||
|
img = img.rotate(270,expand=True)
|
||||||
|
img.save(pdf_path + os.sep + "pdf" + pdf_name[:-4] + ".png")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
gened_list = [] # 新创建的文件列表,在出错时或者生成完毕时进行删除
|
gened_list = [] # 新创建的文件列表,在出错时或者生成完毕时进行删除
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue