我有一个简单的python脚本将文件转换为PDF。它还有另一个功能,它将它们合并到一个顶部导入的PDF文件中。但是,每次运行转换脚本时,即使我没有调用F_merge,它也会运行合并脚本。转换脚本如下。为什么/在哪里调用F_Merge?
# Converts files to PDF
from PIL import Image
import os,glob2,re
from PDF_Merge_Script import F_Merge
def Converter(filename):
im = Image.open(filename)
if im.mode == "RGBA":
im = im.convert("RGB")
if filename[-4:-1] == "jpe":
new_filename = filename[:-4]+'pdf'
if not os.path.exists(new_filename):
im.save(new_filename,"PDF",resolution=100.0)
else:
new_filename = filename[:-3]+'pdf'
if not os.path.exists(new_filename):
im.save(new_filename,"PDF",resolution=100.0)
PNG_files = glob2.glob("*.PNG")
JPEG_files = glob2.glob("*.jpeg")
JPG_files = glob2.glob("*.JPG")
Txt_files = glob2.glob("*.txt")
print(Txt_files)
print(PNG_files)
print(JPEG_files)
print(JPG_files)
这是我的输出。没错。打印的语句来自我的合并函数中的条件。
最初,当前目录中没有.pdf文件或目录
中只存在一个.pdf[' FILE1.TXT']
[]
[]
[]
这是合并脚本
#Merges all .pdf files in the current directory where this scipt is saved
from PyPDF2 import PdfFileMerger
import os,glob2,re
def F_Merge(path):
pdf_files = glob2.glob("*.pdf")
if len(pdf_files) <= 1:
print("Intially, No .pdf files in current directory or only one .pdf
exists in directory")
else:
merger = PdfFileMerger()
for files in pdf_files:
merger.append(path+files)
if not os.path.exists(path+'merged.pdf'):
merger.write(path+'merged.pdf')
merger.close()
print('\n'.join(pdf_files)+"\nmerged to file named merged.pdf in current folder")
path = re.sub(r'\\',r'/',os.getcwd()[2:])+'/'
F_Merge(path)