简体   繁体   English

我正在Windows 10计算机上的python上使用openpyxl库,并尝试使用空白load_workbook

[英]I am using the openpyxl library on python on a windows 10 computer and trying to load_workbook with whitespace

I am using the openpyxl library on python 3.5 on a windows 10 computer and trying to load_workbook() an pathname with white space. 我正在Windows 10计算机上的python 3.5上使用openpyxl库,并尝试load_workbook()具有空格的路径名。

I have looked it up online and can't seem to solve this myself. 我在网上查询过,似乎无法自己解决。 I have included some of my attempts below. 我在下面列出了一些尝试。

I must be missing something completely, because I can't find anyone else asking about this particular issue with openpyxl. 我必须完全缺少一些东西,因为找不到其他人询问有关openpyxl的特定问题。

Any help would be very much appreciated. 任何帮助将不胜感激。

My current work around would be to use python to check and possibly rename the file to remove the spaces, but that just seems entirely unnecessary and i might not always have the permissions to do that. 我当前的解决方法是使用python检查并可能重命名文件以删除空格,但这似乎完全没有必要,而且我可能并不总是具有执行此操作的权限。

import openpyxl
from openpyxl import load_workbook
import os

docName = "space book.xlsx"
docNameWithExits = "space^ book.xlsm"
fullPathOfDocument =  "./" + docName
fullPathOfDocumentExtraPar = "'" + "./" + docName + "'"
fullOsPath =  os.path.join("." , docName)
docObject = open(fullPathOfDocument,"rb")

try:
    attempt1 = load_workbook(docName)
    print("worked")
except:
    print("didnt work")

try:
    attempt2 = load_workbook(fullPathOfDocument)
    print("worked")
except:
    print("didnt work")

try:
    attempt3 = load_workbook(fullPathOfDocumentExtraPar)
    print("worked")
except:
    print("didnt work")

try:
    attempt4 = load_workbook(docNameWithExits)
    print("worked")
except:
    print("didnt work")

try:
    attempt5 = load_workbook(fullOsPathvar)
    print("worked")
except:
    print("didnt work")

try:
    attempt6 = load_workbook(docObject)
    print("worked")
except:
    print("The computer has won")

Thank you in advance for any help Regards, -Nex 预先感谢您的任何帮助问候,-Nex

openpyxl.load_workbook() can read a path with Whitespace in it. openpyxl.load_workbook()可以读取其中包含空格的路径。 In my fully reproducible example, there's whitespace in the workbook name and the folders. 在我的完全可复制的示例中,工作簿名称和文件夹中有空格。

I'm guessing that the problems occur if you are not preceding your strings with an "r". 我猜想如果您的字符串前面没有“ r”,则会出现问题。 The "r" will tell python to treat the given string as a raw string. “ r”将告诉python将给定的字符串视为原始字符串。

import os
from openpyxl import Workbook
from openpyxl import load_workbook

# Path to be created - please change to your desired folder location
path = r"C:\Users\doe_j\test\python\a path\with spaces"
wb_name = path + r"\space book.xlsx"

if os.path.isdir(path) != True:
    os.makedirs(path)
    print(path, "created!")
elif os.path.isdir(path) != False:
    print(path, "already exists!") 


# making a workbook so this example is reproducible
wb = Workbook()
ws = wb.active

ws["A1"] = "Putting some text in"

wb.save(wb_name)

# loading that same workbook 
wb = load_workbook(wb_name)
ws = wb.active

ws["A3"] = "Adding Some more text in"

wb.save(wb_name)

This example was tested on windows 10, python 3.5 and openpyxl 2.4.7. 此示例已在Windows 10,python 3.5和openpyxl 2.4.7上进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 openpyxl - “load_workbook”未定义 - openpyxl - "load_workbook" not defined openpyxl load_workbook() 冻结 - openpyxl load_workbook() freezes 尝试从 openpyxl 使用模块 load_workbook 时出错 - Error when trying to use module load_workbook from openpyxl Openpyxl: 'ValueError: Max value is 14' 使用 load_workbook 时 - Openpyxl: 'ValueError: Max value is 14' when using load_workbook Openpyxl - 对象没有属性“load_workbook” - Openpyxl - object has no attribute 'load_workbook' openpyxl:在 load_workbook() 上给出错误 - openpyxl: Gives error on load_workbook() 我正在尝试使用 Windows 10 上的 python 3.8.3、openpyxl 从单列中的几行传输数据 - I am trying to transfer data from a few rows in a single column using python 3.8.3, openpyxl, on Windows 10 Python openpyxl load_workbook错误:TypeError(NoneType不可迭代)和ValueError(最大值为180) - Python openpyxl load_workbook Errors: TypeError (NoneType not Iterable) and ValueError (Max. Value is 180) 使用python InvalidFileException在excel load_workbook中写入.log:openpyxl不支持.log文件格式,请 - write .log in excel load_workbook with python InvalidFileException: openpyxl does not support .log file format, please 如何在openpyxl中使用“ wb = load_workbook('path',True)”读取特定单元格 - How to read a particular cell by using “wb = load_workbook('path', True)” in openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM