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:
      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:
    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
    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)
    models = gatherModels(rows)
    print(gatherYears(rows, models))
    bCont = gatherYears(rows, models)
    saveToFile(currRow, bCont)
    currRow = currRow + len(rows)
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
looks like it just creates a new empty workbook and saves it.

The 'saveTo' workbook looks like it needs to be saved.
Due to the reasons discussed here we will be moving to on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Return to General Coding Help

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests