推荐一个网址https://pypi.org/
search for “openpyxl” on this website  copy the commander, and paste it on the terminal of PyCharm   不知道为什么我的terminal打开后有红色字体的提示,先不管了,如果报错,再来解决这个问题)  结果,我应该在anaconda安装过了  果然。先看看能不能用吧  1.右键当前工程文件“HelloWorld" 2.open in explorer(系统不同,命名不同,有些是reveal in finder)  copy the xls document in this folder   error 
原来anaconda和pycharm不会共享package,但是anaconda安装以后,pycharm就不能安装了,所以现在anaconda卸载openpyxl
 这次成功安装了,但是安装路径不对,还是安装在anaconda里了  参考https://blog.csdn.net/mukvintt/article/details/80908951   不做了,用spyder,继续干活吧。 如果电脑不是安装了好几个编译器,应该不会出现这种问题。 不管编译器是什么,代码都能用。  新建一个.py文件  并把excel文件复制到该路径下  可运行下列代码读取文件(每一个单元格的值)
import openpyxl as xl
wb=xl.load_workbook("transactions.xlsx")
sheet=wb['Sheet1']
max_column1=sheet.max_column
max_row1=sheet.max_row
for row in range(1,max_row1+1):
for column in range (1, max_column1 +1):
cell=sheet .cell(row, column )
print(cell.value )
数据增加
import openpyxl as xl
wb=xl.load_workbook("transactions.xlsx")
sheet=wb['Sheet1']
max_column1=sheet.max_column
max_row1=sheet.max_row
for row in range (2,max_row1 +1):
cell=sheet.cell(row,3)
corrected_price=cell.value * 0.9
corrected_cell=sheet.cell(row,4)
corrected_cell.value=corrected_price
wb.save("transactions2.xlsx")
资料参考于https://www.youtube.com/watch?v=_uQrJ0TkZlc
|