aaron.yang 发表于 2020-10-4 20:54:11

openpyxl问题

为什么我运行下面代码会报错:
# -*- coding: utf-8 -*-

import openpyxl
from openpyxl.styles import PatternFill

orange = PatternFill(fill_type=None, start_color='#FFA100')

wb = openpyxl.load_workbook(filename="计划表.xlsx")

ws = wb.active

ws.title = "计划表"

ws["A1"].fill = orange

报错内容:
Traceback (most recent call last):
File "D:/files/Aaron/编程/code/python/PycharmProjects/计划表管理/main.py", line 6, in <module>
    orange = PatternFill(fill_type=None, start_color=(0, 255, 161, 0))
File "D:\files\Aaron\编程\code\python\PycharmProjects\计划表管理\venv\lib\site-packages\openpyxl\styles\fills.py", line 91, in __init__
    self.fgColor = fgColor
File "D:\files\Aaron\编程\code\python\PycharmProjects\计划表管理\venv\lib\site-packages\openpyxl\styles\colors.py", line 134, in __set__
    super(ColorDescriptor, self).__set__(instance, value)
File "D:\files\Aaron\编程\code\python\PycharmProjects\计划表管理\venv\lib\site-packages\openpyxl\descriptors\base.py", line 42, in __set__
    raise TypeError('expected ' + str(self.expected_type))
TypeError: expected <class 'openpyxl.styles.colors.Color'>

Process finished with exit code 1

zltzlt 发表于 2020-10-4 21:04:32

# -*- coding: utf-8 -*-

import openpyxl
from openpyxl.styles import PatternFill
from openpyxl.styles.colors import Color

orange = PatternFill(fill_type=None, start_color=Color('00FFA100'))

wb = openpyxl.load_workbook(filename="计划表.xlsx")

ws = wb.active

ws.title = "计划表"

ws["A1"].fill = orange

aaron.yang 发表于 2020-10-4 21:06:39

zltzlt 发表于 2020-10-4 21:04


又报错了:
Traceback (most recent call last):
File "D:/files/Aaron/编程/code/python/PycharmProjects/计划表管理/main.py", line 9, in <module>
    wb = openpyxl.load_workbook(filename="计划表.xlsx")
File "D:\files\Aaron\编程\code\python\PycharmProjects\计划表管理\venv\lib\site-packages\openpyxl\reader\excel.py", line 314, in load_workbook
    data_only, keep_links)
File "D:\files\Aaron\编程\code\python\PycharmProjects\计划表管理\venv\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
File "D:\files\Aaron\编程\code\python\PycharmProjects\计划表管理\venv\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
File "C:\Users\Aaron\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1222, in __init__
    self._RealGetContents()
File "C:\Users\Aaron\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1289, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Process finished with exit code 1

zltzlt 发表于 2020-10-4 21:09:03

aaron.yang 发表于 2020-10-4 21:06
又报错了:

你的 计划表.xlsx 是否能正常打开?

疾风怪盗 发表于 2020-10-4 21:10:56

和你这个fill_type有关吧,而且最后要保存
# -*- coding: utf-8 -*-

import openpyxl
from openpyxl.styles import PatternFill

orange = PatternFill(fill_type="solid",start_color='DC143C')

wb = openpyxl.load_workbook(filename="1.xlsx")

ws = wb.active

#ws.title = "计划表"

ws["B1"].fill = orange
ws['B1'].value='1'
wb.save("1.xlsx")

aaron.yang 发表于 2020-10-4 21:11:16

zltzlt 发表于 2020-10-4 21:09
你的 计划表.xlsx 是否能正常打开?

修复了,重新创建了一个工作簿,成功了,感谢!
但又出现了一个问题:为什么单元格A1并没有变成橙色?

疾风怪盗 发表于 2020-10-6 21:05:38

{:10_284:}这个帖子不打算结一下么。。。。。。。。。。。。
页: [1]
查看完整版本: openpyxl问题