【问题标题】:trouble whit save() in barcode generation in windowsWindows中条形码生成中的save()问题
【发布时间】:2016-05-03 15:01:28
【问题描述】:

您好,我正在开发一个将条形码生成 pdf 文件的应用程序。我在 linux 中尝试过,效果很好,但是在 Windows 中尝试时,我收到了一些错误。 我的代码是下一个:

def crear_barcode(numero):
  filename = 'generated/temp/'+numero
  writer = barcode.writer.ImageWriter()
  code = barcode.Code39(numero,writer,add_checksum = False)
  archivo = code.save(filename)
  return archivo

我收到的错误是:

  1. Traceback (most recent call last): File "C:\Documents and Settings\usuario\Escritorio\NIF-master\nif.py", line 23, in generarButton_clicked generar_codigos(provincia,ciudad,numeroInicial,cantidad) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 64, in generar_codigos archivo.image(crear_barcode(numero),eje_x * 50, linea * 25 , TAMANIO_CODIGO) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 43, in crear_barcode archivo = code.save(filename) File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\base.py", line 69, in save _filename = self.writer.save(filename, output) File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\writer.py", line 291, in save output.save(filename, self.format.upper()) File "C:\Python27\lib\site-packages\PIL\Image.py", line 1681, in save save_handler = SAVE[format.upper()] KeyError: u'PNG'

当我更改 save() 行并给它一个扩展名时,即: code.save(filename,'png') 我收到了

  1. Traceback (most recent call last): File "C:\Documents and Settings\usuario\Escritorio\NIF-master\nif.py", line 23, in generarButton_clicked generar_codigos(provincia,ciudad,numeroInicial,cantidad) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 64, in generar_codigos archivo.image(crear_barcode(numero),eje_x * 50, linea * 25 , TAMANIO_CODIGO) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 43, in crear_barcode archivo = code.save(filename,'png') File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\base.py", line 68, in save output = self.render(options) File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\codex.py", line 105, in render options.update(writer_options or {}) ValueError: dictionary update sequence element #0 has length 1; 2 is required

我不明白为什么会出现在 windows 而不是 linux 中。 我已经安装了所有依赖项,PIL、pyBarcode、pyFpdf。

【问题讨论】:

  • 在第一种情况下,您的包似乎不是为支持 PNG 图像而构建的,而在第二种情况下,传入的选项应该是字典或关键字选项。

标签: python windows python-2.7 barcode


【解决方案1】:

我之前遇到过类似的问题,这就是我为解决它所做的:

1) 打开这个文件 - C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\writer.py

2) 你会看到下面的代码 -

try:
  import Image, ImageDraw, ImageFont ### The Statement to be edited ####
except ImportError:
  try:
    from PIL import Image, ImageDraw, ImageFont  # lint:ok
  except ImportError:
    import sys
    sys.stderr.write('PIL not found. Image output disabled.\n\n')
    Image = ImageDraw = ImageFont = None  # lint:ok

3) 您需要编辑第一个导入语句,使代码如下所示 -

try:
  from PIL import Image, ImageDraw, ImageFont ### Edited.
except ImportError:
  try:
    from PIL import Image, ImageDraw, ImageFont  # lint:ok
  except ImportError:
    import sys
    sys.stderr.write('PIL not found. Image output disabled.\n\n')
    Image = ImageDraw = ImageFont = None  # lint:ok

4) 保存文件。

5) 尝试运行您的应用。

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果没有完整的代码进行测试,这似乎是由于操作系统特定的文件分隔符造成的。 Linux 使用正斜杠,而 Windows 使用反斜杠。尝试使用独立于平台的文件名:

    filename = os.path.join('generated','temp', str(numero))
    

    【讨论】:

    • ehhh 不是问题,我改了,但还是不行。完整代码在这个github repo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2021-12-10
    • 2011-09-27
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多