Openpyxl - 将图像转换为 excel 电子表格后出错 styles.xml 可能存在问题
Openpyxl - Error after convert an image to excel spreadsheet possible problem with styles.xml
我正在尝试使用 python 脚本将图像 (*.jpg) 转换为 Excel 电子表格的背景颜色。根据以下照片:
完整 Python 脚本:
import openpyxl
from PIL import Image
def image_to_excel(file, output, percentage):
# Open picture and create a workbook instance
im = Image.open(file)
wb = openpyxl.Workbook()
sheet = wb.active
# Resize image with spreadsheet's columns
width, height = im.size
cols = width * percentage/100
print('Old image size: ' + str(im.size))
imgScale = cols/width
newSize = (int(width*imgScale), int(height*imgScale))
im = im.resize(newSize)
# Get new picture's dimensions
cols, rows = im.size
print('New image size: ' + str(im.size))
# Spreadsheet's cell: height = 6 and width = 1
for i in range(1, rows):
sheet.row_dimensions[i].height = 0.6
for j in range(1, cols):
column_letter = openpyxl.utils.get_column_letter(j)
sheet.column_dimensions[column_letter].width = 0.0625
# Convert image to RGB
rgb_im = im.convert('RGB')
# Formatting cell's color
for i in range(1, rows):
for j in range(1, cols):
c = rgb_im.getpixel((j, i))
rgb2hex = lambda r,g,b: f"ff{r:02x}{g:02x}{b:02x}"
c = rgb2hex(*c)
sheet.cell(row = i, column = j).value = " "
customFill = openpyxl.styles.PatternFill(start_color=c, end_color=c, fill_type='solid')
sheet.cell(row = i, column = j).fill = customFill
# Save workbook
#im.close()
#rgb_im.close()
wb.save(output)
wb.close()
# Export
image_to_excel('jangada.jpg', 'final.xlsx', 100)
问题是当我尝试更改图像时,像这样:https://www.planetware.com/wpimages/2019/09/croatia-in-pictures-most-beautiful-places-to-visit-plitvice-lakes.jpg 并且在 运行 代码之后出现错误:
翻译是这样的:
Excel 能够通过修复或删除不可读的内容来打开文件。
删除的记录:来自 /xl/styles.xml 部分的样式 (Styles)
修复记录:/xl/worksheets/sheet1.xml
的单元格部分信息
我用的是excel 2013,有人知道怎么解决吗?
您的 rgb2hex
没有按预期工作,请从中删除 ff
,我认为这会破坏您的代码。
你的功能:
>>>rgb2hex = lambda r,g,b: f"ff{r:02x}{g:02x}{b:02x}"
>>>rgb2hex(120,120,120)
ff787878
输出应该是787878
.
>>>rgb2hex = lambda r,g,b: f"{r:02x}{g:02x}{b:02x}"
>>>rgb2hex(120,120,120)
787878
我正在尝试使用 python 脚本将图像 (*.jpg) 转换为 Excel 电子表格的背景颜色。根据以下照片:
完整 Python 脚本:
import openpyxl
from PIL import Image
def image_to_excel(file, output, percentage):
# Open picture and create a workbook instance
im = Image.open(file)
wb = openpyxl.Workbook()
sheet = wb.active
# Resize image with spreadsheet's columns
width, height = im.size
cols = width * percentage/100
print('Old image size: ' + str(im.size))
imgScale = cols/width
newSize = (int(width*imgScale), int(height*imgScale))
im = im.resize(newSize)
# Get new picture's dimensions
cols, rows = im.size
print('New image size: ' + str(im.size))
# Spreadsheet's cell: height = 6 and width = 1
for i in range(1, rows):
sheet.row_dimensions[i].height = 0.6
for j in range(1, cols):
column_letter = openpyxl.utils.get_column_letter(j)
sheet.column_dimensions[column_letter].width = 0.0625
# Convert image to RGB
rgb_im = im.convert('RGB')
# Formatting cell's color
for i in range(1, rows):
for j in range(1, cols):
c = rgb_im.getpixel((j, i))
rgb2hex = lambda r,g,b: f"ff{r:02x}{g:02x}{b:02x}"
c = rgb2hex(*c)
sheet.cell(row = i, column = j).value = " "
customFill = openpyxl.styles.PatternFill(start_color=c, end_color=c, fill_type='solid')
sheet.cell(row = i, column = j).fill = customFill
# Save workbook
#im.close()
#rgb_im.close()
wb.save(output)
wb.close()
# Export
image_to_excel('jangada.jpg', 'final.xlsx', 100)
问题是当我尝试更改图像时,像这样:https://www.planetware.com/wpimages/2019/09/croatia-in-pictures-most-beautiful-places-to-visit-plitvice-lakes.jpg 并且在 运行 代码之后出现错误:
翻译是这样的:
Excel 能够通过修复或删除不可读的内容来打开文件。
删除的记录:来自 /xl/styles.xml 部分的样式 (Styles)
修复记录:/xl/worksheets/sheet1.xml
的单元格部分信息我用的是excel 2013,有人知道怎么解决吗?
您的 rgb2hex
没有按预期工作,请从中删除 ff
,我认为这会破坏您的代码。
你的功能:
>>>rgb2hex = lambda r,g,b: f"ff{r:02x}{g:02x}{b:02x}"
>>>rgb2hex(120,120,120)
ff787878
输出应该是787878
.
>>>rgb2hex = lambda r,g,b: f"{r:02x}{g:02x}{b:02x}"
>>>rgb2hex(120,120,120)
787878