kz26 / PyExcelerate

Accelerated Excel XLSX Writing Library for Python 2/3
https://pypi.org/project/PyExcelerate/
BSD 2-Clause "Simplified" License
530 stars 60 forks source link

raise exception " 'str' object has no attribute 'is_default'" #216

Closed jeffery9 closed 1 year ago

jeffery9 commented 1 year ago

have write a test script to test write excel

from pyexcelerate import Workbook, Color, Style, Font, Fill, Alignment, Borders, Border, Range
import time
import xlsxwriter
import xlwt
import base64
import psycopg2
from io import BytesIO

CELL_TITLE_STYLE = Style(
    fill=Fill(background=Color(184, 204, 228)),
    format='#,##0',
    alignment=Alignment(horizontal='center'),
    font=Font(bold=True, size=9),
    borders=Borders.Borders(left=Border.Border(style='thin'), top=Border.Border(
        style='thin'), bottom=Border.Border(style='thin'), right=Border.Border(style='thin'), )
)

CUSTOMER_INFO_STYLE = Style(
    font=Font(bold=True, size=8),
    fill=Fill(background=Color(1, 255, 255)),
    alignment=Alignment(horizontal='left'),
    borders=Borders.Borders(left=Border.Border(style='thin'), top=Border.Border(
        style='thin'), bottom=Border.Border(style='thin'), right=Border.Border(style='thin'), )
)

MAIN_TITLE_STYLE = Style(
    font=Font(bold=True, size=20, color=Color(255, 255, 255)),
    fill=Fill(background=Color(102, 102, 153)),
    alignment=Alignment(horizontal='center', vertical='center')
)

MAIN_TITLE_FIELD_VALUE_STYLE = Style(
    font=Font(bold=True),
    alignment=Alignment(horizontal='center')
)

MAIN_INFO_STYLE = Style(
    font=Font(size=10),
    alignment=Alignment(horizontal='left', vertical='center'),
    borders=Borders.Borders(left=Border.Border(style='thin'), top=Border.Border(
        style='thin'), bottom=Border.Border(style='thin'), right=Border.Border(style='thin'), )
)

def generate_excel3():
    t1 = time.time()
    sheet_name = '期间对账报表'
    cell_header = [
        '订单编号', '下游单号', '订单日期', '确认日期', '出入库日期', '经销商出入库日期',
        '仓库名称', '下游客户简称', '产品编码', '实发产品', '计价产品', 'SPH', 'CYL', '轴位', '数量',
        '货款', '单据类型', '订单备注1', '订单备注2', '订单备注3', '备注',
    ]
    workbook = Workbook()
    # workbook.add_style(MAIN_TITLE_STYLE)
    # workbook.add_style(CUSTOMER_INFO_STYLE)
    # workbook.add_style(MAIN_TITLE_FIELD_VALUE_STYLE)
    # workbook.add_style(CELL_TITLE_STYLE)
    # workbook.add_style(MAIN_INFO_STYLE)
    worksheet = workbook.new_sheet(sheet_name)
    # worksheet.set_column(1, 20, width=35)
    r0 = Range.Range.coordinate_to_string((1, 1))
    r1 = Range.Range.coordinate_to_string((3, 20))
    print(r0, r1)
    worksheet.range(r0, r1).merge()
    worksheet.set_cell_value(1, 1, sheet_name)
    worksheet.set_cell_style(1, 1, MAIN_TITLE_STYLE)
    worksheet.set_cell_value(5, 1, '经销商编码')
    worksheet.set_cell_style(5, 1, CUSTOMER_INFO_STYLE)
    worksheet.set_cell_value(5, 2, 'x')
    worksheet.set_cell_style(5, 2, MAIN_TITLE_FIELD_VALUE_STYLE)
    worksheet.set_cell_value(5, 3, '经销商名称')
    worksheet.set_cell_style(5, 3, CUSTOMER_INFO_STYLE)
    worksheet.set_cell_value(5, 4, 'x')
    worksheet.set_cell_style(5, 4, MAIN_TITLE_FIELD_VALUE_STYLE)
    worksheet.set_cell_value(7, 1, '确认日期范围')
    worksheet.set_cell_style(7, 1, CUSTOMER_INFO_STYLE)
    worksheet.set_cell_value(7, 2, 'x')
    worksheet.set_cell_style(7, 2, MAIN_TITLE_FIELD_VALUE_STYLE)
    worksheet.set_row_style(8, CELL_TITLE_STYLE)
    worksheet.range('A8', 'U8').value = [cell_header]
    # worksheet.set_row_values(8, cell_header)
    row = [i for i in range(21)]
    for idx in range(55000):
        worksheet.set_row_style(idx+9, MAIN_INFO_STYLE)
        r0 = Range.Range.coordinate_to_string((idx+9, 1))
        r1 = Range.Range.coordinate_to_string((idx+9, 21))
        worksheet.range(r0, r1).value = [row]

    t2 = time.time()
    print('done')
    print('%s' % (t2-t1))
    workbook.save("output.xlsx")

generate_excel3()

but it raise error

Exception has occurred: AttributeError       (note: full exception trace is shown but execution is paused at: <module>)
'str' object has no attribute 'is_default'
  File "/Users/jeffery/Library/Python/3.9/lib/python/site-packages/pyexcelerate/Workbook.py", line 77, in _align_styles
    obj and not obj.is_default
  File "/Users/jeffery/Library/Python/3.9/lib/python/site-packages/pyexcelerate/Workbook.py", line 96, in _save
    self._align_styles()
  File "/Users/jeffery/Library/Python/3.9/lib/python/site-packages/pyexcelerate/Workbook.py", line 103, in save
    self._save(fp)
  File "/Users/jeffery/Downloads/ysl/xls_write.py", line 227, in generate_excel3
    workbook.save("output.xlsx")
  File "/Users/jeffery/Downloads/ysl/xls_write.py", line 230, in <module> (Current frame)
    generate_excel3()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
AttributeError: 'str' object has no attribute 'is_default'