openpyxl 借助 smbprotocol 直接读取 smb 中excel 直接写入 共享盘

go 复制代码
import os.path
from io import BytesIO
from openpyxl import load_workbook, Workbook

from smbclient import \
    (listdir,
     mkdir,
     open_file,
     copyfile,
     register_session,
     rmdir,
     scandir,
     makedirs,
     delete_session)
from smbclient.path import isdir, exists

server = '127.0.0.1'
username = 'username'
password = 'password'
# Optional - register the server with explicit credentials
register_session(server, username=username, password=password)

remote_directory = r'\\127.0.0.1\123'
remote_file = r"\\127.0.0.1\01_result.xlsx"
remote_save_excel = r"\\127.0.0.1\01_save_result.xlsx"

# # Create a directory (only the first request needs credentials)
# mkdir(r"\\server\share\directory", username="user", password="pass")
#
# # Remove a directory
# rmdir(r"\\server\share\directory")
#
# # Checking whether a file is a directory
# d_filename = r"\\server\share\directory"
# print("Is file {} dir?: {}".format(d_filename, isdir(d_filename)))

# List the files/directories inside a dir
# for filename in listdir(r"\\server\share\directory"):
#     print(filename)

# Use scandir as a more efficient directory listing as it already contains info like stat and attributes.
# for file_info in scandir(remote_directory):
#     file_inode = file_info.inode()
#     if file_info.is_file():
#         print("File: %s %d" % (file_info.name, file_inode))
#     elif file_info.is_dir():
#         print("Dir: %s %d" % (file_info.name, file_inode))
#     else:
#         print("Symlink: %s %d" % (file_info.name, file_inode))

exists_status = exists(remote_directory)
print('exists_status', exists_status)
makedirs(remote_directory, exist_ok=True)

with open_file(remote_file, mode='rb') as file:
    excel_data = file.read()


excel_file= excel_file2 = BytesIO(excel_data)
wb = load_workbook(excel_file, read_only=True)

# Access worksheets and cells
worksheet = wb.active
for row in worksheet.iter_rows():
    for cell in row:
        print(cell.value, end='\t')
    print()

wb = Workbook()
worksheet = wb.active
worksheet['A1'] = 'Hello'
worksheet['B1'] = 'World'

# Save Excel data to a BytesIO object
excel_file = BytesIO()
wb.save(excel_file)

# Save Excel data to a file
with open_file(remote_save_excel, mode='wb') as file:
    file.write(excel_file2.getvalue())
    print('write remote excel file success', remote_save_excel)

参考
https://github.com/jborean93/smbprotocol/blob/master/examples/high-level/file-management.py
https://github.com/jborean93/smbprotocol/tree/master/examples

相关推荐
drebander1 分钟前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
乌啼霜满天2494 分钟前
Spring 与 Spring MVC 与 Spring Boot三者之间的区别与联系
java·spring boot·spring·mvc
tangliang_cn9 分钟前
java入门 自定义springboot starter
java·开发语言·spring boot
程序猿阿伟10 分钟前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
Grey_fantasy20 分钟前
高级编程之结构化代码
java·spring boot·spring cloud
弗锐土豆27 分钟前
工业生产安全-安全帽第二篇-用java语言看看opencv实现的目标检测使用过程
java·opencv·安全·检测·面部
Elaine20239128 分钟前
零碎04 MybatisPlus自定义模版生成代码
java·spring·mybatis
小小大侠客1 小时前
IText创建加盖公章的pdf文件并生成压缩文件
java·pdf·itext
一二小选手1 小时前
【MyBatis】全局配置文件—mybatis.xml 创建xml模板
xml·java·mybatis
码老白1 小时前
【老白学 Java】Warshipv2.0(二)
java·网络