问题
I built an iOS app that takes users' inputs then uploads their data as .csv files onto dropbox.
Now, I'd like to build a cross-platform desktop app that uses openpyxl and the Python dropbox api to listen for these .csv uploads and then write the data in these .csv files to various .xlsx workbooks. I have no issues with this, except my client wants to be able to watch on their desktop as the excel workbooks change.
Whenever I make an assignment with openpyxl, such as...
wb = load_workbook("sample.xlsx") #load an existing .xlsx file
ws = wb.active #get active worksheet
ws['A1'] = 12 #assign data to cell A1
... then running this script will successfully alter the file, BUT I have to close and then open the file to see the change, instead of watching the change happen as it happens.
So, my question is, how do I watch openpyxl's changes to excel workbooks happen as they occur, and is there a cross-platform solution for this? Alternate ways of approaching the problem are appreciated!
My research on this has spanned a few hours and the best I have come up with is some hybrid of AppleScript and pywin32, but that could get real ugly/buggy real fast!
I much appreciate the help!
回答1:
I don't think you can do this easily. Excel files are themselves simply zip archives and this makes monitoring very difficult because you cannot modify zip files in place: you always create a new archive. This is exactly why Excel employs file-locking.
It sounds to me like you'd need some kind of external data source that Excel itself can monitor and integrate. You might want to look at xlwings for in place editing. Alternatively, it might be possible to use openpyxl to create an external data source. It will work with external files.
来源:https://stackoverflow.com/questions/33219317/how-to-watch-excel-file-change-while-openpyxl-makes-changes