簡體   English   中英

使用 Python xlrd 可以將貨幣字段轉換為 xlsx 文件中的文本嗎?

[英]With Python xlrd can you convert a money field to text in an xlsx file?

我正在使用 SQL Server 將一些 xlrd 文件批量加載到 SQL 表中,但需要在使用 Python 轉換為 CSV 之前將貨幣字段轉換為文本以刪除逗號。 我可以用 xlrd 做到這一點嗎?

您可以使用如下邏輯:

    import xlrd
    import xlwt
    from xlutils.copy import copy 

    # reading
    book = xlrd.open_workbook('original.xls') # read original excel
    sheet = book.sheet_by_index(0)
    col = 1  # the column where you need to change

    # writing
    wb = copy(book)  # copy
    worksheet = wb.get_sheet(0)

    for row in range(0, sheet.nrows):
        for column in range(0, sheet.ncols):
            if column == col:   # compare here
                val = sheet.cell(row, column).value # fetch value
                val_modified = str(val) # modify value
                worksheet.write(row, column, val_modified) # write to excel

    wb.save('new.xls') # save as new excel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM