python - 对目录中的所有文件运行 python 脚本
问题描述
第一次在这里发布问题,希望有经验/尝试过的人分享您的见解......在过去的几天和晚上,我一直在努力做到这一点......现在我无处可循环这个脚本在目录中的每个文件上。
基本上,这两个脚本工作得很好,它带来了一个 pdf 文件并将其更改为一个 excel 工作簿。现在我需要做的是浏览选定目录中的所有文件并执行相同的工作。
我一直卡在打开文件阶段 - 这是说数据(pdf页面 - 数据[0])不能被调用吗?还是我应该添加更多阶段以将数据集引入...?
我是否必须为数据集创建一个列表,以便我可以调用数据,因为您将拥有多个要调用的数据..这就是 python 可以读取数据 [0] 的原因吗?
修改后的脚本
# import
import os
import glob
import pdftotext
import openpyxl
from pathlib import Path
from string import ascii_uppercase
# open a pdf file
def to_excel(pdf_file):
with open(pdf_file,'rb') as f:
data = pdftotext.PDF(f)
# operate data to get titles, values
datas = data[0].split('\r\n')
finalData = list()
for item in datas:
if item != '':
finalData.append(item)
finalDataRefined = list()
for item in finalData:
if item != ' BCA Scheduled Maintenance Questions' and item != ' Do you suspect there is Asbestos at the property?' and item != ' Yes' and item != ' No' and item != '\x0c':
finalDataRefined.append(item.strip())
titles = list()
values = list()
for num, item in enumerate(finalDataRefined):
if num % 2 == 0:
titles.append(item)
else:
values.append(item)
# get an output file name
OPRAST = values[1]
filename = work_dir / f"{OPRAST}.xlxs"
# create an excel workbook
excel_file = openpyxl.Workbook()
excel_sheet = excel_file.active
excel_sheet.append([])
alphaList = list(ascii_uppercase)
for alphabet in alphaList:
excel_sheet.column_dimensions[alphabet].width = 20
excel_sheet.append(titles)
excel_sheet.append(values)
# save the excel workbook
excel_file.save(filename)
excel_file.close
# run a python script every file in a directory
alphaList = list(ascii_uppercase)
work_dir = Path(r"C:\Users\Sunny Kim\Downloads\Do Forms")
for pdf_file in work_dir.glob("*.pdf"):
to_excel(pdf_file)
解决方案
我基本上知道你想做什么,但是你的代码缩进不是那么可读……尤其是它是 python。
您的目标是为前缀目录中的每个 pdf 文件创建一个 excel?或将所有pdf文件聚合到一个excel文件中?
以下编码是针对第一个目标的。
代码逻辑。
- 获取所有的pdf文件
- 循环遍历所有 pdf 文件,对于每个:
- 打开pdf文件
- 一些操作
- 导出到excel文件
你的完整代码可能是这样的(只是猜测):
# ----------------import part-------------------
import os
import glob
import pdftotext
import openpyxl
from string import ascii_uppercase
from pathlib import Path
def to_excel(pdf_file):
with open(pdf_file, 'rb') as f: # this open the pdf file
data = pdftotext.PDF(f)
# ---------------operate the data, get title and value-----------
datas = data[0].split('\r\n')
finalData = list()
for item in datas:
if item != '':
finalData.append(item)
finalDataRefined = list()
for item in finalData:
if item != ' BCA Scheduled Maintenance Questions' and item != ' Do you suspect there is Asbestos at the property?' and item != ' Yes' and item != ' No' and item != '\x0c':
finalDataRefined.append(item.strip())
titles = list()
values = list()
for num, item in enumerate(finalDataRefined):
if num % 2 == 0:
titles.append(item)
else:
values.append(item)
# ------------------get output file name---------------------
OPRAST = values[1]
filename = work_dir / f"{OPRAST}.xlxs"
# ------------------create excel file sheet------------------
excel_file = openpyxl.Workbook()
excel_sheet = excel_file.active
excel_sheet.append([])
alphaList = list(ascii_uppercase)
for alphabet in alphaList:
excel_sheet.column_dimensions[alphabet].width = 20
excel_sheet.append(titles)
excel_sheet.append(values)
# --------------------save----------------
excel_file.save(filename)
excel_file.close
# -------------------main program---------------
alphaList = list(ascii_uppercase)
work_dir = Path(r"C:\Users\Sunny Kim\Downloads\Do Forms")
for pdf_file in work_dir.glob("*.pdf"):
to_excel(pdf_file)
推荐阅读
- r - 如何使用rentrez从文章中提取作者和链接?
- json - 确定在解组时是否在 json 字符串中提供了值
- php - 使用 DateTime() 获取每月的第 N 天
- kubernetes - 是否可以将多个入口与 IBM Cloud Kubernetes 服务合并?
- r - 从嵌套循环中保存图形和统计信息
- javascript - 从输入字段拍照并转换为base64
- django - 将用户模型中的 first_name、last_name 和 email 设置为字段中的默认值
- sql - 使用拆分的 T-SQL 分组
- python - 输出是数字列表而不是一个数字
- java - 具有该位置 [1] 的参数不存在;嵌套异常是 java.lang.IllegalArgumentException:具有该位置 [1] 的参数不存在