保存在python中使用join(dir_path ..)打开的Excel工作表

时间:2015-08-12 10:29:16

标签: python excel

以下是我的代码:

from os.path import join
from xlutils.copy import copy
from xlrd import open_workbook,cellname
from os.path import join, dirname, abspath
import xlwt


def Trend():
    fname = join(dirname(dirname(abspath(__file__))),'Data Files', 'Processed Data', 'TrendAnalysis.xls')

    # Open the workbook

    book = open_workbook(fname, formatting_info=True)
    wb = copy(book) # a writable copy (I can't read values out of this, only write to it)
    total=0.
    style = xlwt.easyxf('font: bold 1, name Calibri')
    style1 = xlwt.easyxf('font: name Calibri')
    for i in range(2,25):

        if(i==1):
            pass
        else:        
            sheet = book.sheet_by_index(i)
            w_sheet = wb.get_sheet(i) # the sheet to write to within the writable copy

            cols = sheet.ncols   # Number of columns
            rows = sheet.nrows   # Number of rows

            for column in range(1,cols):

                for row in range(1,rows):
                    if(sheet.cell(row,column).value == '-'):
                        pass

                    else:

                        total=total+sheet.cell(row,column).value
                w_sheet.write(row+1, column, total, style1)
                total=0
            w_sheet.write(row+1,0, 'TOTAL', style)
            i=i+1

    s=book.sheet_by_index(0)
    w = wb.get_sheet(0)
    cols = s.ncols   # Number of columns
    rows = s.nrows   # Number of rows
    for row in range(1,rows):
        if(s.cell(row,0).value== "ISU-GOV Domestic"):
            for column in range(0,3):
                a=s.cell(row,column).value
                b=s.cell(21,column).value
                w.write(21,column,a)
                w.write(row,column,b)
        elif(s.cell(row,0).value== "ISU-GOV Overseas"):
            for column in range(0,3):
                a=s.cell(row,column).value
                b=s.cell(23,column).value
                w.write(row,column,b)
                w.write(23,column,a)
        elif(s.cell(row,0).value== "ISU-MFG (TML)"):
            for column in range(0,3):
                a=s.cell(row,column).value
                w.write(24,column,a)
                b=s.cell(20,column).value
                w.write(12,column,b)
        elif(s.cell(row,0).value== "NGM-INDIA"):
            for column in range(0,3):
                a=s.cell(row,column).value
                w.write(25,column,a)

        else:
            c=s.cell(row,0).value
            w.write(row, 0, c)

    for column in range(1,cols):

        for row in range(1,20):
            if(s.cell(row,column).value == '-'):
                pass

            else:

                total=total+s.cell(row,column).value
        w.write(20, column, total, style1)
        total=0
    w.write(20,0, 'SUB TOTAL', style)

    for column in range(1,cols):

        for row in range(20,rows):
            if(s.cell(row,column).value == '-'):
                pass

            else:

                total=total+s.cell(row,column).value
        w.write(26, column, total, style1)
        total=0
    w.write(26,0, 'SUB TOTAL', style)

    for column in range(1,cols):

        for row in range(1,rows):
            if(s.cell(row,column).value == '-'):
                pass

            else:

                total=total+s.cell(row,column).value
        w.write(27, column, total, style1)
        total=0
    w.write(27,0, 'GRAND TOTAL', style)    




    wb.save('fname')

对excel文件所做的更改不会得到反映。编译不会给出错误,但是在excel表上没有进行任何更改。你能帮我解决一下这个问题。

1 个答案:

答案 0 :(得分:0)

程序中的行说:

wb.save('fname')

将保存到名为' fname'。

的文件中

您想要使用您的fname变量,而不是文字字符串' fname',以便该行应显示为:

wb.save(fname)