用python打开pdf文件格式

2024-09-25 18:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试用python 2.7打开100个pdf文件,代码如下:

import arcpy,fnmatch,os

rootPath = r"D:\desktop"
pattern = '*.pdf'
counter = 0
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        os.startfile(rootPath)
        counter = counter + 1
print counter

结果,rootPath文件夹打开,python打印pdf文件的数量:

^{pr2}$

没有打开pdf文件。我在论坛里搜索,没有找到任何问题可以回答我的请求。谢谢你的帮助


Tags: 文件代码inimportforpdfoscounter
2条回答

你总是打电话来

os.startfile(rootPath)

其中rootPath只有"D:\desktop"。必须以PDF文件的路径作为参数调用os.startfile。在

^{pr2}$

我不知道你想做什么,但是os.startfile将打开adobepdf阅读器(或其他设置为默认阅读器的阅读器)。。。我是如何做到这一点的,而且似乎奏效了。在

import os

rootPath = "D:\\desktop"
counter = 0
for file in os.listdir(rootPath):
    if file.endswith('.pdf'):
        os.startfile("%s/%s" %(rootPath, file))
        counter = counter + 1
print counter

或者不需要大量编辑主代码

^{pr2}$

相关问题 更多 >