forked from Dawn_Ocean/ZJUEVA-Reimburse
增加了文件夹中存在多个 .pdf 文件,选择发票文件的功能
parent
bd63d92c50
commit
5d7f8c0772
29
main.py
29
main.py
|
|
@ -16,7 +16,7 @@ def convert_all(path = os.path.abspath('.')):
|
|||
items = os.listdir(path)
|
||||
for item in items:
|
||||
if os.path.isdir(item):
|
||||
result = convert_img(path + '\\' + item)
|
||||
result = convert_img(item, path + '\\' + item)
|
||||
if result == 1:
|
||||
print("PDF 转换失败!")
|
||||
return
|
||||
|
|
@ -26,19 +26,38 @@ def convert_all(path = os.path.abspath('.')):
|
|||
print("转换完毕!")
|
||||
|
||||
"""传入绝对路径"""
|
||||
def convert_img(path = os.path.abspath('.')):
|
||||
def convert_img(dir_name, path = os.path.abspath('.')):
|
||||
files = os.listdir(path)
|
||||
pdf_list = []
|
||||
for file in files:
|
||||
file = file.lower() # 排除 .PNG 等带来的问题
|
||||
if file.endswith('.pdf'):
|
||||
result = pdf2img(path, file)
|
||||
if result: # 1 -> Error
|
||||
return 1
|
||||
pdf_list.append(file)
|
||||
elif file.endswith('.jpg') or file.endswith('.jpeg'):
|
||||
img = Image.open(path + '\\' + file)
|
||||
dot_index = file.rfind('.')
|
||||
img.save(path + '\\' + file[dot_index:] + ".png", "PNG")
|
||||
|
||||
img.save(path + '\\' + file[:dot_index] + ".png", "PNG")
|
||||
if len(pdf_list) > 1:
|
||||
print(f"注意到文件夹 {dir_name} 中有多个 .pdf 文件:")
|
||||
for i in range(len(pdf_list)): # 打印 pdf 文件和选项
|
||||
print(f"{i + 1} - {pdf_list[i]}") # 索引从 0 改为 1
|
||||
while True:
|
||||
invoice_index = input("请选择发票文件(输入选项前的阿拉伯数字):")
|
||||
if not invoice_index.isdigit():
|
||||
print("请输入数字!")
|
||||
continue
|
||||
else:
|
||||
invoice_int = int(invoice_index) - 1 # 索引从 1 改为 0
|
||||
if invoice_int < len(pdf_list) and invoice_int >= 0:
|
||||
invoice_path = 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"
|
||||
break
|
||||
else:
|
||||
print("请选择正确的序号!")
|
||||
|
||||
def gen_filelist(path = os.path.abspath('.')):
|
||||
print("创建文件夹列表中...")
|
||||
|
|
@ -81,7 +100,7 @@ def gen_docx(path = os.path.abspath('.')):
|
|||
for dir, dir_file in file_list.items():
|
||||
parent_path = path + '\\' + dir + '\\'
|
||||
for file in dir_file:
|
||||
if file[0:3] == "pdf":
|
||||
if file[0:3] == "Pdf":
|
||||
dir_file.remove(file)
|
||||
dir_file.insert(0, file)
|
||||
doc.add_picture(parent_path + dir_file[0], height = Inches(2.5))
|
||||
|
|
|
|||
Loading…
Reference in New Issue