首页 > 解决方案 > ascii 编解码器无法解码位置 11 中的字节 0x96:序数不在范围内(128)

问题描述


这是我的错误回溯(最近一次调用最后一次):文件“\08srv\acad\PO Summaries\TimeForce\TF2POS.py”,第 120 行,在 PyApp() 文件“\08srv\acad\PO Summaries\TimeForce\TF2POS. py",第 24 行,在初始化中 store = self.create_model() 文件“\08srv\acad\PO Summaries\TimeForce\TF2POS.py”,第 112 行,在 create_model workbook.close() 文件“C:\Python27\lib\site-packages\xlsxwriter\workbook .py”,第 297 行,关闭 self._store_workbook() 文件“C:\Python27\lib\site-packages\xlsxwriter\workbook.py”,第 605 行,_store_workbook xml_files = packager._create_package() 文件“C: \Python27\lib\site-packages\xlsxwriter\packager.py”,第 139 行,在 _create_package self._write_shared_strings_file() 文件“C:\Python27\lib\site-packages\xlsxwriter\packager.py”,第 286 行,在_write_shared_strings_file sst._assemble_xml_file() 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”,第 53 行,在 _assemble_xml_file self._write_sst_strings() 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”,第 83 行,在 _write_sst_strings self._write_si(string) 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings .py”,第 110 行,在 _write_si self._xml_si_element(string, attributes) 文件“C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py”,第 122 行,在 _xml_si_element self.fh.write("" "%s""" % (attr, string)) 文件“C:\Python27\lib\codecs.py”,第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\ codecs.py",第 369 行,写入数据,消耗 = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 11: ordinal not in range(128)\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”,第 83 行,在 _write_sst_strings self._write_si(string) 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”,第 110 行,在 _write_si self._xml_si_element(string, attributes) 文件 "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py",第 122 行,在 _xml_si_element self.fh.write("""%s""" % (attr, string)) 文件“C:\Python27\lib\codecs.py”,第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,在写入数据中,消耗 = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 11: ordinal not in range(128)\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”,第 83 行,在 _write_sst_strings self._write_si(string) 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”,第 110 行,在 _write_si self._xml_si_element(string, attributes) 文件 "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py",第 122 行,在 _xml_si_element self.fh.write("""%s""" % (attr, string)) 文件“C:\Python27\lib\codecs.py”,第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,在写入数据中,消耗 = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 11: ordinal not in range(128)在 _write_sst_strings self._write_si(string) 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”中,第 110 行,在 _write_si self._xml_si_element(string, attributes) 文件“C:\Python27\lib\ site-packages\xlsxwriter\xmlwriter.py",第 122 行,在 _xml_si_element self.fh.write("""%s""" % (attr, string)) 文件 "C:\Python27\lib\codecs.py" ,第 706 行,在写入中返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,在写入数据中,消耗 = self.encode(object, self.errors) UnicodeDecodeError: “ascii”编解码器无法解码位置 11 中的字节 0x96:序数不在范围内(128)在 _write_sst_strings self._write_si(string) 文件“C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py”中,第 110 行,在 _write_si self._xml_si_element(string, attributes) 文件“C:\Python27\lib\ site-packages\xlsxwriter\xmlwriter.py",第 122 行,在 _xml_si_element self.fh.write("""%s""" % (attr, string)) 文件 "C:\Python27\lib\codecs.py" ,第 706 行,在写入中返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,在写入数据中,消耗 = self.encode(object, self.errors) UnicodeDecodeError: “ascii”编解码器无法解码位置 11 中的字节 0x96:序数不在范围内(128)在 _write_si self._xml_si_element(string, attributes) 文件 "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py",第 122 行,在 _xml_si_element self.fh.write("""%s""" % (attr, string)) 文件“C:\Python27\lib\codecs.py”,第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,在写入数据中,消耗 = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 11: ordinal not in range(128)在 _write_si self._xml_si_element(string, attributes) 文件 "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py",第 122 行,在 _xml_si_element self.fh.write("""%s""" % (attr, string)) 文件“C:\Python27\lib\codecs.py”,第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,在写入数据中,消耗 = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 11: ordinal not in range(128)第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,写入数据,消耗 = self.encode(object, self.errors) UnicodeDecodeError: ' ascii'编解码器无法解码位置 11 中的字节 0x96:序数不在范围内(128)第 706 行,写入返回 self.writer.write(data) 文件“C:\Python27\lib\codecs.py”,第 369 行,写入数据,消耗 = self.encode(object, self.errors) UnicodeDecodeError: ' ascii'编解码器无法解码位置 11 中的字节 0x96:序数不在范围内(128)


* 1.从 SQL SERVER 的数据
创建工作簿 2.创建 Excel 工作簿 3.将这些工作簿链接到主 Excel 表*

#!/usr/bin/python
# TimeForce
# username: sa
# password: Dr8gedog

import os.path
import sys
import shutil
import pyodbc
import datetime
import xlsxwriter

global store
global column_sort
column_sort = 0
global adj
global value
value = 0

class PyApp(): 
    def __init__(self):
        global store
        store = self.create_model()

        return
        #gtk.timeout_add( 60*1000, self.my_timer ) # Every 30 seconds

    def create_model(self):
        cursor.execute("SELECT JobNumber, JobName, JobDescription, User1, complete_yn, JobCode, User2, User3 FROM Job ORDER BY JobName")
        rows = cursor.fetchall()

        cy = datetime.datetime.year
        cm = datetime.datetime.month
        cd = datetime.datetime.day

#CREATING WORKBOOK
        workbook = xlsxwriter.Workbook('\\\\08SRV\\acad\\PO Summaries\\TEST.xlsx')
        #workbook = xlsxwriter.Workbook('\\\\08SRV\\acad\\PO Summaries\\PO Summary Home Page.xlsx')
        worksheet = workbook.add_worksheet()

        cy = datetime.datetime.year
        cm = datetime.datetime.month
        cd = datetime.datetime.day

        center = workbook.add_format({'align': 'center'})
        header = workbook.add_format({'font_size': 14, 'bold': 1, 'align': 'center'})
        url_format = workbook.add_format({'font_color': 'blue', 'underline': 1, 'align': 'center'})

        col_job = 0 #A
        col_customer = 1 #B
        col_description = 2 #C
        col_leadforeman = 3 #D
        col_pm = 4 #E
        col_drafter = 5 #F
        col_customerpo = 6 #G
        col_jobstatus = 7 #H

        worksheet.write_string(0, col_job, "Job #", header)
        worksheet.write_string(0, col_customer, "Customer", header)
        worksheet.write_string(0, col_leadforeman, "Lead Foreman", header)
        worksheet.write_string(0, col_description, "Description", header)
        worksheet.write_string(0, col_customerpo, "Customer PO #", header)
        worksheet.write_string(0, col_jobstatus, "Job Status", header)
        worksheet.write_string(0, col_pm, "P.M.", header)
        worksheet.write_string(0, col_drafter, "Drafter", header)


        worksheet.set_column('A:A', 15)
        worksheet.set_column('B:B', 30)
        worksheet.set_column('C:C', 60)
        worksheet.set_column('D:D', 21)
        worksheet.set_column('E:E', 10)
        worksheet.set_column('F:F', 15)
        worksheet.set_column('G:G', 25)
        worksheet.set_column('H:H', 20)

        row = 1
        for i in rows:
            s = i[1].split(" - ")
            job_number = str(i[0])
            po_link = "file:///\\\\08SRV\\acad\\PO Summaries\\Purchase Order Summaries\\" + job_number + ".xlsx"
            worksheet.write_url(row, col_job, po_link, url_format, job_number)
            worksheet.write_string(row, col_customer, s[0].strip())
            worksheet.write_string(row, col_description, i[2])
            worksheet.write_string(row, col_leadforeman, i[5])
            worksheet.write_string(row, col_pm, i[6])
            worksheet.write_string(row, col_drafter, i[7])
            worksheet.write_string(row, col_customerpo, i[3])
            if (i[4] == False):
                worksheet.write_string(row, col_jobstatus, "Active", center)
                if (os.path.exists("\\\\08srv\\acad\\PO Summaries\\Purchase Order Summaries")):
                    try:
                        os.makedirs("\\\\08srv\\acad\\PO Summaries\\Shop Drawings\\" + job_number)
                    except:
                        pass
                    if (os.path.exists("\\\\08srv\\acad\\PO Summaries\\Purchase Order Summaries\\" + job_number + ".xlsx")):
                        #print("File exists")
                        pass
                    else:
                        shutil.copy("\\\\08srv\\acad\\PO Summaries\\Purchase Order Summaries\\PO Template.xlsx", "\\\\08srv\\acad\\PO Summaries\\Purchase Order Summaries\\" + job_number + ".xlsx")
            else :
                worksheet.write_string(row, col_jobstatus, "Dead", center)
                worksheet.set_row(row, options={'hidden': True})
            row = row + 1

        worksheet.freeze_panes(1, 0)
        worksheet.autofilter(0, 0, row, col_jobstatus)
        worksheet.filter_column_list('H', ['Active'])

        #FILE.close()
        workbook.close()
        print("DONE.")
        return True
    #print("File exists")

# Check: Administrative Tools > Data Sources (ODBC) > User DNS.
cnxn = pyodbc.connect('DSN=joblist')
cursor = cnxn.cursor()
PyApp()
cnxn.close()
#sys.exit()

标签: pythonsql-server

解决方案


推荐阅读