Get the inputs from Excel and use those inputs in python script

后端 未结 4 1336
予麋鹿
予麋鹿 2021-01-16 14:23

How to get the inputs from excel and use those inputs in python.

相关标签:
4条回答
  • 2021-01-16 14:41

    Not sure if this is exactly what you're talking about, but:

    If you have a very simple excel file (i.e. basically just one table filled with string-values, nothing fancy), and all you want to do is basic processing, then I'd suggest just converting it to a csv (comma-seperated value file). This can be done by "saving as..." in excel and selecting csv.

    This is just a file with the same data as the excel, except represented by lines seperated with commas: cell A:1, cell A:2, cell A:3 cell B:1, cell B:2, cell b:3

    This is then very easy to parse using standard python functions (i.e., readlines to get each line of the file, then it's just a list that you can split on ",").

    This if of course only helpful in some situations, like when you get a log from a program and want to quickly run a python script which handles it.

    Note: As was pointed out in the comments, splitting the string on "," is actually not very good, since you run into all sorts of problems. Better to use the csv module (which another answer here teaches how to use).

    0 讨论(0)
  • 2021-01-16 14:54

    If you can save as a csv file with headers:

    Attrib1, Attrib2, Attrib3
    value1.1, value1.2, value1.3
    value2,1,...
    

    Then I would highly recommend looking at built-in the csv module

    With that you can do things like:

    csvFile = csv.DictReader(open("csvFile.csv", "r"))
    for row in csvFile:
        print row['Attrib1'], row['Attrib2']
    
    0 讨论(0)
  • 2021-01-16 14:57

    Take a look at xlrd

    This is the best reference I found for learning how to use it: http://www.dev-explorer.com/articles/excel-spreadsheets-and-python

    0 讨论(0)
  • 2021-01-16 14:57
    import win32com
    
    Excel=win32com.client.Dispatch("Excel.Application")
    Excel.Workbooks.Open(file path) 
    Cells=Excel.ActiveWorkBook.ActiveSheet.Cells
    Cells(row,column).Value=Input
    Output=Cells(row,column).Value
    
    0 讨论(0)
提交回复
热议问题