【发布时间】:2021-09-20 01:06:59
【问题描述】:
我正在使用 Python 脚本将不同文件夹中的许多图像批量转换为单个 pdf(使用 https://pypi.org/project/img2pdf/):
import os
import subprocess
import img2pdf
from shutil import copyfile
def main():
folders = [name for name in os.listdir(".") if os.path.isdir(name)]
for f in folders:
files = [f for f in os.listdir(f)]
p = ""
for ffile in files:
p += f+'\\' + ffile + " "
os.system("py -m img2pdf *.pn* " + p + " --output " + f + "\combined.pdf")
if __name__ == '__main__':
main()
但是,尽管在 Windows 10 上通过 Powershell 运行命令,并且尽管使用了非常短的文件名,但当图像数量非常多(例如超过 600 个左右)时,Powershell 会给我错误“命令行太长"并且它不会创建 pdf。我知道有一个命令行字符串限制(https://docs.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/command-line-string-limitation),但我也知道对于 powershell,这个限制更高(Powershell to avoid cmd 8191 character limit),我不知道如何修复脚本。我想问你是否可以帮助我修复脚本以避免违反字符限制。谢谢
PS:我将脚本插入到包含图像文件夹的父文件夹中后使用该脚本;然后在每个子文件夹中创建输出 pdf 文件。
【问题讨论】:
-
你能把你的数组分解成多个更小的数组并多次调用应用程序吗?
-
如果您使用
img2pdf作为库,您可以避免不得不处理命令行(及其限制)。 -
我可以尝试,但我不是很有经验,我不知道该怎么做:您能建议我对代码进行哪些更改吗?
-
项目描述页面有一些例子。但如果你连尝试都不愿意,我会花时间帮助那些愿意尝试的人。
-
是的,我见过,可惜我对python知之甚少:我还没有创建脚本。
标签: python powershell command-line limit img2pdf