Openpyxl Save

This is the place for queries that don't fit in any of the other categories.

Openpyxl Save

Postby tsutttra12 » Tue Jan 28, 2014 7:21 pm

I have this code written to manipulate excel data. the code runs fine yet the document it's supposed to write and save to is still blank
Code: Select all
from openpyxl import load_workbook
from openpyxl.workbook import *
exResource = load_workbook(R'C:\Users\Evans002\Desktop\ASGExports\ASG_Export_1.xlsx')
sheet = exResource.get_sheet_by_name('Sheet2')
saveTo = load_workbook(R'C:\Users\Evans002\Desktop\pythonOutputFitment.xlsx')
currRow = 2
numOfRows = 3195
def gatherRows(currRow):
   rows = []
   testingRow = currRow
   currCont = sheet.cell('A'+str(currRow)).value
   testCont = sheet.cell('A'+str(testingRow)).value
   while currCont == testCont:
      rows.append(testingRow)
      testingRow = testingRow + 1
      testCont = sheet.cell('A'+str(testingRow)).value
   return rows
def gatherModels(focusedRows):
    models = []
    for i in focusedRows:
        iCont = sheet.cell('B'+str(i)).value
        if iCont not in models:
           
            models.append(iCont)
    return models
   

def gatherYears(rows, models):
    years = {}
    for p in models:
        years[p] = []
    for i in rows:
        iContB = sheet.cell('B'+str(i)).value
        iContC = sheet.cell('C'+str(i)).value
        years[iContB].append(iContC)
    return years
       
   
def saveToFile(currRow, dit):
    saveSheet = saveTo.get_sheet_by_name('Sheet')
    saveSheet.cell('A'+str(currRow)).value = sheet.cell('A'+ str(currRow)).value
    saveSheet.cell('B'+str(currRow)).value = str(dit)
   
while currRow <= numOfRows:
   
    rows = gatherRows(currRow)
    print(rows)
    models = gatherModels(rows)
   
    print(gatherYears(rows, models))
    bCont = gatherYears(rows, models)
    saveToFile(currRow, bCont)
    currRow = currRow + len(rows)
Workbook().save(R'C:\Users\Evans002\Desktop\pythonOutputFitment.xlsx')
tsutttra12
 
Posts: 8
Joined: Fri Jan 17, 2014 4:44 pm

Re: Openpyxl Save

Postby Yoriz » Tue Jan 28, 2014 9:28 pm

I havent used Openpyxl but the line
Code: Select all
Workbook().save(R'C:\Users\Evans002\Desktop\pythonOutputFitment.xlsx')
looks like it just creates a new empty workbook and saves it.

The 'saveTo' workbook looks like it needs to be saved.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 724
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 6 guests