Solved! Go to Solution.
from openpyxl import load_workbook wb = load_workbook(filename = r'Excel2013.xlsx') sheet_ranges = wb.get_sheet_by_name(name = 'Sheet1') print sheet_ranges.cell('C3').value # C3
import win32com.client xl = win32com.client.Dispatch('Excel.Application') xl.Visible = 1 wb = xl.Workbooks.Add() datasheet = wb.Worksheets.Add() datasheet.Name = 'New name for my worksheet' datasheet.Cells(1, 1).Value = 'This is cell A1' datasheet.Cells(2, 2).Value = 'This is cell B2' wb.SaveAs('C:\\Temp\\example.xlsx') wb.Close(SaveChanges=1) xl.Quit() del xl
# Test to see if ws32com can read a Excel 2013 spreadsheet # testcomread.py # import the Dispatch library, get a reference to an Excel instance from win32com.client import Dispatch import os xlApp = Dispatch("Excel.Application") # Set the Excel file name, working directory and path old_file_name = 'Excel2013.xlsx' working_dir = r"C:\Users\Paul\My Documents\pyscripts\Office" + os.sep old_file_path = os.path.join(working_dir, old_file_name) xlBook = xlApp.Workbooks.Open(old_file_path) # make Excel visible (1 is True, 0 is False) xlApp.Visible=1 # get a reference to the first sheet xlSheet = xlBook.Sheets(1) # extract the value from a cell a_value = xlSheet.Cells(1,1).Value print a_value
I know this post was done forever ago but it has been helpful to me. However, I now need to be able to delete the first column in the spreadsheet but I haven't been able to find code for that. Can you point me in the right direction?
The easiest way I have done it personally is to use the readline() method, split that line into a list, return the new one by indexing out the first column and then joining and writing the line out to a new file.
try: with open(fin, 'r') as inf:
with open(fout, 'w') as outf:
for line in inf:
lineToCheck = line #variable the for loop to hold the line from fin
correctedLine = body(lineToCheck) # corrected line String
outf.write(correctedLine)
...and the function to deal with it...
def body(lineCheck😞
'''Parses through the lines of the csv removing the first column.'''
tempLine = lineCheck.split(',') # creates a list from the parsed input line
tempLine = tempLine[1:] # drops the "0" position in the list and returns the rest
returnString = ','.join(tempLine) # rejoins the line
return returnString # returns it back to main script
Thanks Jason. I'll give this a try.