简体   繁体   English

Python 3 xlutils + xlrd + xlwt

[英]Python 3 xlutils+xlrd+xlwt

I'm struggling with editing existing xls workbook with xlrd/xlwt/xlutils I can change values in xlrd workbook, but I need to write them in existing workbook. 我正在努力使用xlrd / xlwt / xlutils编辑现有的xls工作簿,我可以更改xlrd工作簿中的值,但是我需要在现有工作簿中编写它们。 My code doesn't work, I know that alghorytm is right, but code isn't. 我的代码不起作用,我知道alghorytm是正确的,但代码却不正确。

  rb = open_workbook(filename)
#set active sheet
rs = rb.sheet_by_index(0)
#make instance for xlsutils
wb = copy(rb)
ws = wb.get_sheet(0)

rows = rs.nrows
cols = rs.ncols
#iterate and prepare format for SQL db tables
for row_idx in range (0,rows):
    for col_idx in range (0,cols):
        cell=rs.cell(row_idx,col_idx)
        clval = cell.value
        cltp = cell.ctype
        if cltp == xlrd.XL_CELL_BLANK:
           clval ="xy"
           ws.write(row_idx,col_idx,"xy")
        elif cltp == xlrd.XLDateError:
            date_format = XFStyle()
            date_format.num_format_str = 'dd/MM/yyyy'
            clval = '01/01/2018'
            ws.write(row_idx,col_idx, '01/01/2018', date_format)
        elif cltp == xlrd.XL_CELL_EMPTY:
            clval="XYU"
            ws.write(row_idx,col_idx,"xy")
        elif cltp == xlrd.XL_CELL_NUMBER:
            if clval < 0:
             clval=0
             ws.write(row_idx, col_idx,0)
        elif cltp == xlrd.XL_CELL_TEXT:
             clval = ftfy.fix_text(clval)
             ws.write(row_idx,col_idx, ftfy.fix_text(clval))

        elif clval == -693594:
             date_format = XFStyle()
             date_format.num_format_str = 'dd/MM/yyyy'
             clval='01/01/2018'
             ws.write(row_idx, col_idx,'01/01/2018',date_format)
             #print (row_idx,col_idx)
        print(clval)
        save(wb,'abbcards_2.xls')

Output:
 File "D:/rs_al/IdeaProjects/ExcelToSQL/PyXLSSQL/XLStoSQL.py", line 69, in 
 xls_wrk
save(wb,'abbcards_2.xls')
File "C:\Python\lib\site-packages\xlutils\save.py", line 24, in save
StreamWriter(stream)
File "C:\Python\lib\site-packages\xlutils\filter.py", line 941, in process
reader(chain[0])
File "C:\Python\lib\site-packages\xlutils\filter.py", line 65, in __call__
filter.workbook(workbook,filename)
File "C:\Python\lib\site-packages\xlutils\filter.py", line 291, in workbook
self.wtbook.dates_1904 = rdbook.datemode
AttributeError: 'Workbook' object has no attribute 'datemode'

I suppose that I print existing in memory instance of xlrd workbook, values are right. 我想我打印xlrd工作簿的内存实例中存在的值正确。 But how write them? 但是怎么写呢?

I can't understand what copy from xlutils really does. 我不明白从xlutils复制的内容到底是做什么的。 Does it make another object? 它会变成另一个物体吗? If so, how to use xlrd+xlwt for writing? 如果是这样,如何使用xlrd + xlwt进行编写? I found out that pandas can't be used, because I need to know exactly cell type, to change value there. 我发现不能使用大熊猫,因为我需要确切地知道单元格类型,才能在那里更改值。

In java I did same thing with one package where were methods to read/write/save within same object. 在Java中,我对一个包做了同样的事情,其中​​有在同一个对象内读取/写入/保存的方法。

The only working way: 唯一的工作方式:

  1. Open workbook with xlrd 用xlrd打开工作簿
  2. Iterate and get cell values 迭代并获取单元格值
  3. Create new Workbook with xlwt 使用xlwt创建新的工作簿

Through iteration copy cell values 通过迭代复制单元格值

For dates create a proper style for Excel dd/mm/yyyy. 对于日期,请为Excel dd / mm / yyyy创建适当的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM