簡體   English   中英

xlsxwriter 創建不可讀的文件(python)

[英]xlsxwriter creating files that are unreadable (python)

因此,我創建了一個程序,該程序通過將文檔的單元格與像素相同的顏色來獲取圖像並將其復制到 excel 文檔中。 這是我的代碼,

from PIL import Image
import xlsxwriter

image = xlsxwriter.Workbook('Image.xlsx')

image_sheet = image.add_worksheet()

with Image.open("11111.jpg") as px:
    cookie = px.load()
cookie2 = Image.open("11111.jpg")
image_pixels = 0
image_pixels_2 = 0
cookie_height = cookie2.height
cookie_width = cookie2.width
image_sheet.set_column(0, cookie_width, 2.14)

while image_pixels <= cookie_height - 1:
     print(image_pixels)
     while image_pixels_2 <= cookie_width - 1:
          rgb = '#%02x%02x%02x' % cookie[image_pixels_2, image_pixels]
          cell_format = image.add_format()
          cell_format.set_shrink()
          cell_format.set_bg_color(rgb)
          image_sheet.write(image_pixels, image_pixels_2, ' ', cell_format)
          image_pixels_2 += 1

     image_pixels += 1
     if image_pixels_2 >= cookie_width:
          image_pixels_2 = 0

image.close()

它會生成文件,但是當我打開 excel 文件時,它說它不可讀並刪除所有格式。 我不確定為什么會這樣。 該文件有時是可讀的,但有時不是。

Excel 在一個文件中有 64,000 個唯一格式的限制 XlsxWriter 刪除/替換重復格式,但您的程序可能超出 64k 格式限制。

您可以通過像這樣更改程序來打印出使用的唯一格式的數量來檢查:

unique = {}
while image_pixels <= cookie_height - 1:
     print(image_pixels)
     while image_pixels_2 <= cookie_width - 1:
          rgb = '#%02x%02x%02x' % cookie[image_pixels_2, image_pixels]
          unique[rgb] = 1
          cell_format = image.add_format()
          cell_format.set_shrink()
          cell_format.set_bg_color(rgb)
          image_sheet.write(image_pixels, image_pixels_2, ' ', cell_format)
          image_pixels_2 += 1

     image_pixels += 1
     if image_pixels_2 >= cookie_width:
          image_pixels_2 = 0

image.close()
print('Unique fomats = ', len(unique))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM