How to get Python script to write to existing sheet



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:



#import the writer
import xlwt
#import the reader
import xlrd
#open the sussex results spreadsheet
book = xlrd.open_workbook('sussex.xlsx')
#open the first sheet
first_sheet = book.sheet_by_index(0)
#print the values in the second column of the first sheet
print first_sheet.col_values(1)
#in cell 0,0 (first cell of the first row) write "NIF"
sheet1.write(0, 6, "NIF")
#in cell 0,0 (first cell of the first row) write "Points scored"
sheet1.write(0, 6, "Points scored")


On line 12 I get an error:



name 'sheet1' is not defined


How do I define sheet 1 within the sheet that I have already opened?










share|improve this question




























    -1















    I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:



    #import the writer
    import xlwt
    #import the reader
    import xlrd
    #open the sussex results spreadsheet
    book = xlrd.open_workbook('sussex.xlsx')
    #open the first sheet
    first_sheet = book.sheet_by_index(0)
    #print the values in the second column of the first sheet
    print first_sheet.col_values(1)
    #in cell 0,0 (first cell of the first row) write "NIF"
    sheet1.write(0, 6, "NIF")
    #in cell 0,0 (first cell of the first row) write "Points scored"
    sheet1.write(0, 6, "Points scored")


    On line 12 I get an error:



    name 'sheet1' is not defined


    How do I define sheet 1 within the sheet that I have already opened?










    share|improve this question
























      -1












      -1








      -1








      I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:



      #import the writer
      import xlwt
      #import the reader
      import xlrd
      #open the sussex results spreadsheet
      book = xlrd.open_workbook('sussex.xlsx')
      #open the first sheet
      first_sheet = book.sheet_by_index(0)
      #print the values in the second column of the first sheet
      print first_sheet.col_values(1)
      #in cell 0,0 (first cell of the first row) write "NIF"
      sheet1.write(0, 6, "NIF")
      #in cell 0,0 (first cell of the first row) write "Points scored"
      sheet1.write(0, 6, "Points scored")


      On line 12 I get an error:



      name 'sheet1' is not defined


      How do I define sheet 1 within the sheet that I have already opened?










      share|improve this question














      I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:



      #import the writer
      import xlwt
      #import the reader
      import xlrd
      #open the sussex results spreadsheet
      book = xlrd.open_workbook('sussex.xlsx')
      #open the first sheet
      first_sheet = book.sheet_by_index(0)
      #print the values in the second column of the first sheet
      print first_sheet.col_values(1)
      #in cell 0,0 (first cell of the first row) write "NIF"
      sheet1.write(0, 6, "NIF")
      #in cell 0,0 (first cell of the first row) write "Points scored"
      sheet1.write(0, 6, "Points scored")


      On line 12 I get an error:



      name 'sheet1' is not defined


      How do I define sheet 1 within the sheet that I have already opened?







      python excel xlwt






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 11:22









      J4GJ4G

      114110




      114110






















          2 Answers
          2






          active

          oldest

          votes


















          1














          sheet1 is never declared. Try changing it to



          #import the writer
          import xlwt
          #import the reader
          import xlrd
          #open the sussex results spreadsheet
          book = xlrd.open_workbook('sussex.xlsx')
          #open the first sheet
          first_sheet = book.sheet_by_index(0)
          #print the values in the second column of the first sheet
          print first_sheet.col_values(1)
          #in cell 0,0 (first cell of the first row) write "NIF"
          first_sheet.write(0, 6, "NIF")
          #in cell 0,0 (first cell of the first row) write "Points scored"
          first_sheet.write(0, 6, "Points scored")


          edit: You could also use Pandas to read and write to Excel:



          import pandas as pd
          import numpy as np
          #open the sussex results spreadsheet, first sheet is used automatically
          df = pd.read_excel('sussex.xlsx')

          #print the values in the second column of the first sheet
          print(df.iloc[:,1])

          #Create column 'NIF'
          df['NIF'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          #in cell 0,7 (first cell of the first row) write "Points scored"
          df['Points scored'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          <.... Do whatever calculations you want with NIF and Points scored ...>
          # Write output
          df.to_excel('sussex.xlsx')





          share|improve this answer

























          • thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

            – J4G
            Nov 15 '18 at 12:06


















          1














          I guess you need to have something like
          sheet1 = book.sheet_by_index(0); because now sheet1 is not defined.
          Also, document is opened using xlrd which is reader, and you need to write there values - so document should be opened also using xlwt.






          share|improve this answer

























          • Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

            – J4G
            Nov 15 '18 at 11:30












          • Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

            – kosist
            Nov 15 '18 at 11:34











          • that makes sense - how would i open it using xlwt too?

            – J4G
            Nov 15 '18 at 12:07











          • Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

            – kosist
            Nov 15 '18 at 12:27











          • so i need to read from sussex.xlsx and write to a new worksheet?

            – J4G
            Nov 15 '18 at 12:30











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318383%2fhow-to-get-python-script-to-write-to-existing-sheet%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          sheet1 is never declared. Try changing it to



          #import the writer
          import xlwt
          #import the reader
          import xlrd
          #open the sussex results spreadsheet
          book = xlrd.open_workbook('sussex.xlsx')
          #open the first sheet
          first_sheet = book.sheet_by_index(0)
          #print the values in the second column of the first sheet
          print first_sheet.col_values(1)
          #in cell 0,0 (first cell of the first row) write "NIF"
          first_sheet.write(0, 6, "NIF")
          #in cell 0,0 (first cell of the first row) write "Points scored"
          first_sheet.write(0, 6, "Points scored")


          edit: You could also use Pandas to read and write to Excel:



          import pandas as pd
          import numpy as np
          #open the sussex results spreadsheet, first sheet is used automatically
          df = pd.read_excel('sussex.xlsx')

          #print the values in the second column of the first sheet
          print(df.iloc[:,1])

          #Create column 'NIF'
          df['NIF'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          #in cell 0,7 (first cell of the first row) write "Points scored"
          df['Points scored'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          <.... Do whatever calculations you want with NIF and Points scored ...>
          # Write output
          df.to_excel('sussex.xlsx')





          share|improve this answer

























          • thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

            – J4G
            Nov 15 '18 at 12:06















          1














          sheet1 is never declared. Try changing it to



          #import the writer
          import xlwt
          #import the reader
          import xlrd
          #open the sussex results spreadsheet
          book = xlrd.open_workbook('sussex.xlsx')
          #open the first sheet
          first_sheet = book.sheet_by_index(0)
          #print the values in the second column of the first sheet
          print first_sheet.col_values(1)
          #in cell 0,0 (first cell of the first row) write "NIF"
          first_sheet.write(0, 6, "NIF")
          #in cell 0,0 (first cell of the first row) write "Points scored"
          first_sheet.write(0, 6, "Points scored")


          edit: You could also use Pandas to read and write to Excel:



          import pandas as pd
          import numpy as np
          #open the sussex results spreadsheet, first sheet is used automatically
          df = pd.read_excel('sussex.xlsx')

          #print the values in the second column of the first sheet
          print(df.iloc[:,1])

          #Create column 'NIF'
          df['NIF'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          #in cell 0,7 (first cell of the first row) write "Points scored"
          df['Points scored'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          <.... Do whatever calculations you want with NIF and Points scored ...>
          # Write output
          df.to_excel('sussex.xlsx')





          share|improve this answer

























          • thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

            – J4G
            Nov 15 '18 at 12:06













          1












          1








          1







          sheet1 is never declared. Try changing it to



          #import the writer
          import xlwt
          #import the reader
          import xlrd
          #open the sussex results spreadsheet
          book = xlrd.open_workbook('sussex.xlsx')
          #open the first sheet
          first_sheet = book.sheet_by_index(0)
          #print the values in the second column of the first sheet
          print first_sheet.col_values(1)
          #in cell 0,0 (first cell of the first row) write "NIF"
          first_sheet.write(0, 6, "NIF")
          #in cell 0,0 (first cell of the first row) write "Points scored"
          first_sheet.write(0, 6, "Points scored")


          edit: You could also use Pandas to read and write to Excel:



          import pandas as pd
          import numpy as np
          #open the sussex results spreadsheet, first sheet is used automatically
          df = pd.read_excel('sussex.xlsx')

          #print the values in the second column of the first sheet
          print(df.iloc[:,1])

          #Create column 'NIF'
          df['NIF'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          #in cell 0,7 (first cell of the first row) write "Points scored"
          df['Points scored'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          <.... Do whatever calculations you want with NIF and Points scored ...>
          # Write output
          df.to_excel('sussex.xlsx')





          share|improve this answer















          sheet1 is never declared. Try changing it to



          #import the writer
          import xlwt
          #import the reader
          import xlrd
          #open the sussex results spreadsheet
          book = xlrd.open_workbook('sussex.xlsx')
          #open the first sheet
          first_sheet = book.sheet_by_index(0)
          #print the values in the second column of the first sheet
          print first_sheet.col_values(1)
          #in cell 0,0 (first cell of the first row) write "NIF"
          first_sheet.write(0, 6, "NIF")
          #in cell 0,0 (first cell of the first row) write "Points scored"
          first_sheet.write(0, 6, "Points scored")


          edit: You could also use Pandas to read and write to Excel:



          import pandas as pd
          import numpy as np
          #open the sussex results spreadsheet, first sheet is used automatically
          df = pd.read_excel('sussex.xlsx')

          #print the values in the second column of the first sheet
          print(df.iloc[:,1])

          #Create column 'NIF'
          df['NIF'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          #in cell 0,7 (first cell of the first row) write "Points scored"
          df['Points scored'] = np.nan #I don't know what you want to do with this column, so I filled it with NaN's
          <.... Do whatever calculations you want with NIF and Points scored ...>
          # Write output
          df.to_excel('sussex.xlsx')






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 9 '18 at 9:33









          marc_s

          585k13011251272




          585k13011251272










          answered Nov 15 '18 at 11:31









          Niels HenkensNiels Henkens

          795519




          795519












          • thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

            – J4G
            Nov 15 '18 at 12:06

















          • thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

            – J4G
            Nov 15 '18 at 12:06
















          thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

          – J4G
          Nov 15 '18 at 12:06





          thanks @Niels, that seems to have worked but I have now encountered an issue where it can't write - how would i open the workbook to write to it?

          – J4G
          Nov 15 '18 at 12:06













          1














          I guess you need to have something like
          sheet1 = book.sheet_by_index(0); because now sheet1 is not defined.
          Also, document is opened using xlrd which is reader, and you need to write there values - so document should be opened also using xlwt.






          share|improve this answer

























          • Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

            – J4G
            Nov 15 '18 at 11:30












          • Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

            – kosist
            Nov 15 '18 at 11:34











          • that makes sense - how would i open it using xlwt too?

            – J4G
            Nov 15 '18 at 12:07











          • Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

            – kosist
            Nov 15 '18 at 12:27











          • so i need to read from sussex.xlsx and write to a new worksheet?

            – J4G
            Nov 15 '18 at 12:30















          1














          I guess you need to have something like
          sheet1 = book.sheet_by_index(0); because now sheet1 is not defined.
          Also, document is opened using xlrd which is reader, and you need to write there values - so document should be opened also using xlwt.






          share|improve this answer

























          • Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

            – J4G
            Nov 15 '18 at 11:30












          • Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

            – kosist
            Nov 15 '18 at 11:34











          • that makes sense - how would i open it using xlwt too?

            – J4G
            Nov 15 '18 at 12:07











          • Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

            – kosist
            Nov 15 '18 at 12:27











          • so i need to read from sussex.xlsx and write to a new worksheet?

            – J4G
            Nov 15 '18 at 12:30













          1












          1








          1







          I guess you need to have something like
          sheet1 = book.sheet_by_index(0); because now sheet1 is not defined.
          Also, document is opened using xlrd which is reader, and you need to write there values - so document should be opened also using xlwt.






          share|improve this answer















          I guess you need to have something like
          sheet1 = book.sheet_by_index(0); because now sheet1 is not defined.
          Also, document is opened using xlrd which is reader, and you need to write there values - so document should be opened also using xlwt.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 11:33

























          answered Nov 15 '18 at 11:27









          kosistkosist

          8171521




          8171521












          • Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

            – J4G
            Nov 15 '18 at 11:30












          • Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

            – kosist
            Nov 15 '18 at 11:34











          • that makes sense - how would i open it using xlwt too?

            – J4G
            Nov 15 '18 at 12:07











          • Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

            – kosist
            Nov 15 '18 at 12:27











          • so i need to read from sussex.xlsx and write to a new worksheet?

            – J4G
            Nov 15 '18 at 12:30

















          • Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

            – J4G
            Nov 15 '18 at 11:30












          • Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

            – kosist
            Nov 15 '18 at 11:34











          • that makes sense - how would i open it using xlwt too?

            – J4G
            Nov 15 '18 at 12:07











          • Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

            – kosist
            Nov 15 '18 at 12:27











          • so i need to read from sussex.xlsx and write to a new worksheet?

            – J4G
            Nov 15 '18 at 12:30
















          Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

          – J4G
          Nov 15 '18 at 11:30






          Thanks - I think that may have worked but I now get ''Sheet' object has no attribute 'write'

          – J4G
          Nov 15 '18 at 11:30














          Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

          – kosist
          Nov 15 '18 at 11:34





          Please, check the update answer - you open document using xlrd, so that's why sheet object does not have write method.

          – kosist
          Nov 15 '18 at 11:34













          that makes sense - how would i open it using xlwt too?

          – J4G
          Nov 15 '18 at 12:07





          that makes sense - how would i open it using xlwt too?

          – J4G
          Nov 15 '18 at 12:07













          Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

          – kosist
          Nov 15 '18 at 12:27





          Check this example - stackoverflow.com/a/40022335/6917446, it is described there...

          – kosist
          Nov 15 '18 at 12:27













          so i need to read from sussex.xlsx and write to a new worksheet?

          – J4G
          Nov 15 '18 at 12:30





          so i need to read from sussex.xlsx and write to a new worksheet?

          – J4G
          Nov 15 '18 at 12:30

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318383%2fhow-to-get-python-script-to-write-to-existing-sheet%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20