Python:TypeError:预期的字符串或类似字节的对象

时间:2019-07-09 13:10:53

标签: python ubuntu openpyxl

每当尝试通过openpyxl保存excel工作簿时,我都会收到错误消息。不过,只有在添加图像后才会发生这种情况,仅在编辑单元格值后才能保存就可以了。

这是我的剧本

import os
import sys
from openpyxl import load_workbook
import openpyxl

file_path = os.path.dirname(os.path.realpath(__file__)) + "/empty.xlsx"

wb = load_workbook(file_path)
ws = wb.worksheets[0]

def insert_text(search_term,img_path):

  for x in range(1,201):
    for y in range(1,101):
      if isinstance(ws.cell(row=x, column=y).value,str):
        if  ws.cell(row=x, column=y).value == search_term:
          img = openpyxl.drawing.image.Image(img_path)
          img.anchor = ws["A1"]
          # img.anchor = ws.cell(row=(x-2),column=y)
          ws.add_image(img)
          print("Image should have been added")

insert_text("redacted","redacted.jpg")
# insert_text("redacted",os.path.dirname(os.path.realpath(__file__)) + "/signature_redacted.jpg")
# insert_text("For kunde",os.path.dirname(os.path.realpath(__file__)) + "/signature_redacted.jpg")


# insert_text("redacted","New text comes here")

print("Saving to " + os.path.dirname(os.path.realpath(__file__)) + "/empty2.xlsx")
wb.save(os.path.dirname(os.path.realpath(__file__)) + "/empty2.xlsx")

print("1")
sys.stdout.flush()

这是包含我的打印语句的错误。

Image should have been added
Saving to /var/www/redacted/uploads/empty2.xlsx
Traceback (most recent call last):
  File "script.py", line 32, in <module>
    wb.save(str(os.path.dirname(os.path.realpath(__file__))) + str("/empty2.xlsx"))
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/workbook/workbook.py", line 397, in save
    save_workbook(self, filename)
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/writer/excel.py", line 294, in save_workbook
    writer.save()
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/writer/excel.py", line 276, in save
    self.write_data()
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/writer/excel.py", line 76, in write_data
    self._write_worksheets()
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/writer/excel.py", line 219, in _write_worksheets
    self._write_drawing(ws._drawing)
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/writer/excel.py", line 142, in _write_drawing
    self._archive.writestr(drawing.path[1:], tostring(drawing._write()))
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/drawing/spreadsheet_drawing.py", line 283, in _write
    anchor = _check_anchor(obj)
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/drawing/spreadsheet_drawing.py", line 224, in _check_anchor
    row, col = coordinate_to_tuple(anchor)
  File "/usr/local/lib/python3.5/dist-packages/openpyxl/utils/cell.py", line 199, in coordinate_to_tuple
    match = COORD_RE.split(coordinate)
TypeError: expected string or bytes-like object

它在今天早些时候起作用,然后我开始对其进行修补。我没有做备份。

0 个答案:

没有答案