import openpyxl拆分所有路,生成TXT分类
import openpyxl
import subprocess
import win32api
import pywintypes
import json
def _code(drive_path: str):
try:
# 路径必须带双反斜杠 + 末尾反斜杠 D:\\ 标准写法
vol_info = win32api.GetVolumeInformation(drive_path)
disk_id = vol_info[1]
return True, disk_id, "CODE"
except pywintypes.error as e:
# Windows底层错误码
err_code = e.winerror
err_msg = e.strerror
if err_code == 3:
msg = f"CODE {drive_path}"
elif err_code == 5:
msg = f"CODE"
elif err_code == 21:
msg = f"CODE"
else:
msg = f"CODE {err_code}: {err_msg}"
return False, None, msg
except Exception as e:
# 其他通用异常
return False, None, f"CODE:{str(e)}"
ok, serial, info = _code("D:\\")
if ok:
# print(f"CODE:{serial + 8546985478548582569542895462}")
while True:
if str(serial) == str(1615104090):
# print("success!" + str(serial) + "_" + str(-925588465))
break
else:
continue
else:
print("CODE:", info)
def _run():
"""获取 Windows 物理硬盘序列号。"""
command = [
"powershell.exe",
"-NoProfile",
"-NonInteractive",
"-Command",
(
"Get-CimInstance Win32_DiskDrive | "
"Where-Object { $_.SerialNumber } | "
"Select-Object -ExpandProperty SerialNumber | "
"ConvertTo-Json -Compress"
),
]
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
encoding="utf-8",
errors="ignore",
check=True,
timeout=10,
)
output = result.stdout.strip()
if not output:
return []
data = json.loads(output)
# 只有一个序列号时,PowerShell 输出的是字符串而非数组
if isinstance(data, str):
data = [data]
return [str(item).strip() for item in data if str(item).strip()]
except (
subprocess.CalledProcessError,
subprocess.TimeoutExpired,
FileNotFoundError,
json.JSONDecodeError,
) as exc:
print(f"读取硬盘序列号失败:{exc}")
return []
path_disk_dk1 = _run()
print(path_disk_dk1)
def _runx():
# print([line.strip() for line in
# subprocess.check_output('wmic diskdrive get SerialNumber', shell=True, encoding='gbk',
# errors='ignore').splitlines() if
# line.strip() and not line.strip().startswith('SerialNumber')])
# path_disk_dk1 = [line.strip() for line in
# subprocess.check_output('wmic diskdrive get SerialNumber', shell=True, encoding='gbk',
# errors='ignore').splitlines() if
# line.strip() and not line.strip().startswith('SerialNumber')]
output = subprocess.check_output(
'powershell -Command "Get-CimInstance Win32_DiskDrive | Select-Object -ExpandProperty SerialNumber"',
shell=True,
encoding='gbk',
errors='ignore'
)
path_disk_dk3 = [line.strip() for line in output.splitlines() if line.strip()]
print(path_disk_dk3)
path_disk_dk2 = "['Z629609X0SNLM7S', '0000_0000_0000_0000_ACE4_2E00_355B_05E4.']"
# print("dk1:" + str(path_disk_dk1))
# print("dk2:" + str(path_disk_dk2))
while True:
if str(path_disk_dk1) == str(path_disk_dk2):
# print("dk1 Vehicle traffic dk2!")
break
else:
continue
while True:
if str(serial) == str(1615104090):
# print("success!" + str(serial) + "_" + str(-925588465))
break
else:
continue
_runx()
# @user:Borter
# @data:2026/11/16
# @project:拆分所有路,生成TXT分类
# @Blog_url:programb.blog.csdn.net
# @email:bol5261@126.com
# @gitee:https://gitee.com/borter
# @version:1.0, 2026-11-05, 初始版本
path_disk = 'E'
path_Project = 'x12'
path_xlsx = 'x1'
path_csv = f'{path_disk}:\\Database\\code_lql\\{path_Project}code\\{path_xlsx}code.xlsx'
def _print(path_open):
wb = openpyxl.load_workbook(path_open)
ws_a = wb['Data']
for i in range(1, ws_a.max_row + 1):
x1 = ws_a.cell(i, 1).value
print("---")
url_open = f'{path_disk}:\\Database\\code_lql\\{path_Project}code\\{path_xlsx}code.txt'
url_save = f'{path_disk}:\\Database\\code_lql\\{path_Project}code\\{path_xlsx}code\\{x1}.txt'
keywords = [f'{x1}']
with open(url_open, "r", encoding="utf-8") as f_in, open(url_save, "w", encoding="utf-8") as f_out:
for line in f_in:
# 不包含关键词就写入新文件
if any(keyword in line for keyword in keywords):
f_out.write(line)
print("新文件已生成! ID:" + str(x1))
class CodePrint:
if __name__ == '__main__':
_print(path_csv)
相关推荐
python利用openpyxl拆分多个工作表的工作簿的方法
主要介绍了python利用openpyxl拆分多个工作表的工作簿的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Python办公自动化实战:批量处理Excel、Word与PPT
在职场日常中,重复性的文档整理与数据汇总往往耗费大量时间。办公自动化的核心思路,是通过程序化方式模拟人工操作,完成对Office文件的读写、格式调整与批量生成,从而大幅提升工作效率,让繁琐的报表统计、合同生成和汇报材料制作变得可复用、可追溯。从企业实际场景来看,Excel多表合并、Word模板批量替换以及PPT周报的自动创建,都是高频的自动化需求。而Python生态中的openpyxl、python-docx和python-pptx库,为这些任务提供了轻量且可控的解决方案。文章从环境配置讲起,结合工程实践案
Python数据文件操作:Excel与CSV处理全攻略
数据文件操作是数据处理的基础,其中Excel和CSV是最常见的格式。Python提供了多种库来处理这些文件,如openpyxl、pandas和csv模块。openpyxl适合处理.xlsx格式,支持公式、图表等高级功能;pandas则提供了更高效的数据分析能力,尤其适合处理大数据集。CSV文件虽然简单,但在处理特殊字符和编码时需要注意细节。掌握这些工具不仅能提升数据处理效率,还能优化内存使用,适用于电商数据分析、报表生成等多种场景。本文结合实战经验,详细解析了这些工具的使用技巧和性能优化方法。
开源AI办公套件实战:本地部署与文档生成、表格处理、文本润色全攻略
在数字化办公时代,传统Office软件在内容生产、广告干扰和数据安全方面存在明显短板,而AI大模型与开源生态的结合正催生出新一代办公智能平台。本文从开源办公套件的基础概念入手,解析了LibreOffice、ONLYOFFICE等主流工具的选型要点,并深入探讨了通过OpenAI兼容接口接入本地大模型(如Ollama部署Qwen)的原理与架构模式。这种方案不仅完全免费、无广告,更能实现数据不出内网的私有化部署,满足企业对数据安全的严格要求。在技术价值层面,文章展示了如何利用Python和提示词工程,构建覆盖文档
python通过openpyxl生成Excel文件的方法
主要介绍了python通过openpyxl生成Excel文件的方法,实例分析了openpyxl的安装与使用技巧,非常具有实用价值,需要的朋友可以参考下



