简体   繁体   English

Streamskill 和使用 openpyxl 加载工作簿

[英]Streamskill and loading a workbook using openpyxl

I'm new to the Stackoverflow community and I'm currently trying to figure out how to load a workbook into openpyxl using IDLE 3.9.1.我是 Stackoverflow 社区的新手,目前正在尝试弄清楚如何使用 IDLE 3.9.1 将工作簿加载到 openpyxl 中。

I'm trying to follow a tutorial that I purchased from Streamskill which was actually pretty good until I ran into this problem and have spent multiple hours trying to figure out where I went wrong.我正在尝试按照从 Streamskill 购买的教程进行操作,该教程实际上非常好,直到我遇到了这个问题并且花了几个小时试图找出我哪里出错了。

Thought it had something to do with a file location but it is saved in my Python39 folder.以为它与文件位置有关,但它保存在我的 Python39 文件夹中。 the xlsx file in question is just a blank template from Google Sheets titled "Monthly Budget" I've attached a picture of it if that helps.有问题的 xlsx 文件只是 Google 表格中标题为“每月预算”的空白模板,如果有帮助,我附上了一张图片。

Thanks and hope to hear from you soon,谢谢,希望能尽快收到您的来信,

Ben Monthly Budget本月预算

>>> import openpyxl
>>> import os
>>> os.getcwd()
'C:\\Users\\Ben\\AppData\\Local\\Programs\\Python\\Python39'
>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename='budget.xlsx')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    workbook = load_workbook(filename='budget.xlsx')
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 282, in read
    self.read_worksheets()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets
    ws_parser.bind_all()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\_reader.py", line 443, in bind_all
    self.bind_merged_cells()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\_reader.py", line 380, in bind_merged_cells
    self.ws._clean_merge_range(mcr)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\worksheet.py", line 607, in _clean_merge_range
    mcr.format()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\merge.py", line 109, in format
    if side.style is None:
AttributeError: 'NoneType' object has no attribute 'style'

The traceback says that openpyxl found the.xlsx file, opened it, and read it.回溯说 openpyxl 找到了 .xlsx 文件,打开它并读取它。 It had a problem in interpreting it to display it.它在解释它以显示它时遇到了问题。 In particular, the variable side has the Python value None, which means that side.style does not exist.特别是变量side有Python值None,表示side.style不存在。 Some possibilities:一些可能性:

  1. The file is buggy in having data that openpyxl interprets as None.该文件在具有 openpyxl 解释为无的数据方面存在问题。 To check this, try to open the file with Microsoft Excel or some other programs that can read Excel.xlsx files.要检查这一点,请尝试使用 Microsoft Excel 或其他一些可以读取 Excel.xlsx 文件的程序打开文件。 For PCs, the Microsoft Store has a free XLSX Opener program.对于 PC,Microsoft Store 提供免费的 XLSX Opener 程序。 https://www.microsoft.com/en-us/p/xlsx-viewer-free/9nblggh6hbf4?activetab=pivot:overviewtab https://www.microsoft.com/en-us/p/xlsx-viewer-free/9nblggh6hbf4?activetab=pivot:overviewtab

  2. openpyxl is buggy in interpreting the.xlsx data as None. openpyxl 在将 .xlsx 数据解释为 None 时存在问题。

  3. openpyxl is buggy in thinking that side must not be None and in either preventing or not catching the AttributeError. openpyxl 在认为那side不能是 None 并且在防止或不捕获 AttributeError 方面是错误的。 You could try changing你可以尝试改变
    File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\merge.py", line 109 by adding side is not None or after if to change if side.style is None: to if side is not None or side.style is None: .文件“C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\merge.py”,第 109 行通过添加side is not None orif之后更改if side.style is None: to if side is not None or side.style is None:

(The IDE you use to send code to Python should not affect any of this. To make absolutely sure, enter the same statement in the Python command line shell.) (The IDE you use to send code to Python should not affect any of this. To make absolutely sure, enter the same statement in the Python command line shell.)

Thank you to @Terry and @Grijesh I was able to change the version of openpyxl I was using in cmd pip install openpyxl==3.0.4 and follow along the tutorial from Streamskill.感谢@Terry 和@Grijesh 我能够更改我在 cmd pip 中使用的 openpyxl 的版本, pip install openpyxl==3.0.4并按照 Streamskill 的教程进行操作。

Thanks for your help!谢谢你的帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM