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

这篇具有很好参考价值的文章主要介绍了openpyxl 借助 smbprotocol 直接读取 smb 中excel 直接写入 共享盘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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文章来源地址https://www.toymoban.com/news/detail-647775.html

到了这里,关于openpyxl 借助 smbprotocol 直接读取 smb 中excel 直接写入 共享盘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

原文地址:https://blog.csdn.net/nongcunqq/article/details/132213340

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包