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
Thanks Jason. I'll give this a try.
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
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?
# 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
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
Downloading https://pypi.python.org/packages/source/s/setuptools/setuptools-0.9.7.tar.gz Extracting in c:\users\paul\appdata\local\temp\tmptm3fqf Now working in c:\users\paul\appdata\local\temp\tmptm3fqf\setuptools-0.9.7 Installing Setuptools Something went wrong during the installation. See the error message above. Traceback (most recent call last): File "C:\Users\Paul\Documents\pyscripts\ez_setup.py", line 264, in <module> sys.exit(main()) SystemExit: 2
Double check your xlrd version and test on a simple xlsx file.
from mmap import mmap,ACCESS_READ from xlrd import open_workbook print open_workbook('simple.xls') with open('simple.xls','rb') as f: print open_workbook( file_contents=mmap(f.fileno(),0,access=ACCESS_READ) ) aString = open('simple.xls','rb').read() print open_workbook(file_contents=aString)
from xlrd import open_workbook,XL_CELL_TEXT book = open_workbook('simple.xlsx') sheet = book.sheet_by_index(1) cell = sheet.cell(0,0) print cell print cell.value print cell.ctype==XL_CELL_TEXT for i in range(sheet.ncols): print sheet.cell_type(1,i),sheet.cell_value(1,i)
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.