Python Forum
Python script for excel sheet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script for excel sheet
#5
I often have to do stuff like this.

Make yourself a function, put it in a module if you do this very often!

import openpyxl

# where is my file?
path = '/home/pedro/summer2021/19BE/'
# which file?
myfile = 'test_copy_column.xlsx'
# open the file
targetFile = openpyxl.load_workbook(path + myfile)
# sheet names are always handy
targetFileSheetNames = targetFile.sheetnames

# get the values in column 1 and write them to column 2
# could also add them or do other tricks
for sheet in targetFileSheetNames:
    # always need the max_row or max_column to do things like this
    maxRow = targetFile[sheet].max_row
    for rowNum in range(1, maxRow + 1):
        content = targetFile[sheet].cell(row=rowNum, column=1).value
        # this will overwrite without warning if column 2 already has content!!
        targetFile[sheet].cell(row=rowNum, column=2, value=content)

# don't forget to save it        
targetFile.save(path + myfile)
Reply


Messages In This Thread
Python script for excel sheet - by Nabil - May-30-2021, 01:30 PM
RE: Python script for excel sheet - by jefsummers - May-30-2021, 04:39 PM
RE: Python script for excel sheet - by Nabil - May-31-2021, 10:41 AM
RE: Python script for excel sheet - by jefsummers - May-31-2021, 12:41 PM
RE: Python script for excel sheet - by Pedroski55 - Jun-01-2021, 05:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,842 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,199 Jun-29-2023, 11:57 AM
Last Post: gologica
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,908 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 1,667 May-11-2022, 07:12 AM
Last Post: normanwolf
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 7,050 Jan-13-2022, 10:20 PM
Last Post: randolphoralph
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 2,228 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,340 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Append excel sheet using openpyxl TammyP 1 2,373 Feb-02-2021, 06:32 PM
Last Post: nilamo
  How to Copy Single Value From One Excel Sheet to Another SunWers 4 4,322 Dec-29-2020, 05:39 PM
Last Post: SunWers
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,676 Dec-16-2020, 05:26 AM
Last Post: Vokofe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020