13268112009 发表于 2019-10-21 20:42:10

import openpyxl

#打开文件

wb = openpyxl.load_workbook('D:/XXX')
ws = wb.get_sheet_by_name('XXXX')

#修改评分列
ws[''] = x1
ws[''] = x2
ws[''] = x3


#保存修改后的文件
wb.save(r'D:/XXXX')

IRIETP 发表于 2019-10-23 10:05:20

ws['B2'].value="9.7"
ws['B4'].value="9.6"
ws['B5'].value="9.8"

IRIETP 发表于 2019-10-23 10:29:09

为什么我copy这代码,但是运行出错,是什么问题??不理解each_row.value = data_updates.value]?为什么不是等于data_updates.value?

下面是出错的界面:
runfile('C:/Users/coco/.spyder-py3/temp.py', wdir='C:/Users/coco/.spyder-py3')
Traceback (most recent call last):

File "<ipython-input-1-ffe1257465b1>", line 1, in <module>
    runfile('C:/Users/coco/.spyder-py3/temp.py', wdir='C:/Users/coco/.spyder-py3')

File "D:\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

File "D:\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/coco/.spyder-py3/temp.py", line 26, in <module>
    main()

File "C:/Users/coco/.spyder-py3/temp.py", line 16, in main
    wb = openpyxl.load_workbook("豆瓣TOP250电影.xlsx")

File "D:\Anaconda\lib\site-packages\openpyxl\reader\excel.py", line 311, in load_workbook
    data_only, keep_links)

File "D:\Anaconda\lib\site-packages\openpyxl\reader\excel.py", line 126, in __init__
    self.archive = _validate_archive(fn)

File "D:\Anaconda\lib\site-packages\openpyxl\reader\excel.py", line 98, in _validate_archive
    archive = ZipFile(filename, 'r')

File "D:\Anaconda\lib\zipfile.py", line 1204, in __init__
    self.fp = io.open(file, filemode)

FileNotFoundError: No such file or directory: '豆瓣TOP250电影.xlsx'

今天也想吃肠粉 发表于 2019-10-23 16:14:11

看看怎么样

zququ 发表于 2019-10-27 14:40:55

看一下

轻烟 发表于 2019-11-13 19:21:34

大概明白

蓝漠花海 发表于 2019-11-14 20:05:13

import openpyxl

chang_scoring = dict(阿甘正传 = 9.8,这个杀手不太冷 = 9.6,肖申克的救赎 = 9.7)

wb = openpyxl.load_workbook(r'd://aaa//work file//豆瓣TOP250电影.xlsx')
ws = wb]

count = len(chang_scoring)

for each_columns in ws.rows:
    if count == 0:
      break;
    if each_columns.value in chang_scoring:
      ws['B' + str(each_columns.row)].value = str(chang_scoring.value])
      count -= 1
      
wb.save(r'd://aaa//work file//豆瓣TOP250电影.xlsx')

marine-sea 发表于 2019-11-30 11:10:45

查看参考代码

bld007009 发表于 2019-12-8 14:59:55

老师好

18516280477 发表于 2019-12-12 11:16:28

查看内容!

Forever_slient 发表于 2019-12-15 10:29:57

看看思路

newbison 发表于 2019-12-15 17:07:36

~~~

肮脏py交易 发表于 2019-12-19 11:47:26

{:5_103:}

潇洒小小鸟 发表于 2019-12-19 19:12:58

123

zedi 发表于 2019-12-23 13:51:29

回复看看

jinlovelive 发表于 2019-12-23 15:05:06

朕想知道

jschina 发表于 2019-12-23 21:42:55


肖申克的救赎
霸王别姬
这个杀手不太冷

tsungyu1985 发表于 2019-12-30 12:59:21

import openpyxl

wb = openpyxl.load_workbook(r"E:\py_code\豆瓣TOP250.xlsx")
ws = wb['Sheet']

#遍历所有的列,当符合条件时,去修改同行的评分栏位为9.8

for each_row in ws.rows:
    if(each_row.value=='肖申克的救赎'):
      each_row.value='9.8'
      break

wb.save(r"E:\py_code\豆瓣TOP250.xlsx")

这个是我的解法,来看看答案是否有更好的方式 {:10_256:}

tsungyu1985 发表于 2019-12-30 13:00:56

suishoufang 发表于 2018-4-25 11:47
执行:wb.get_sheet_names()和 ws = wb.get_sheet_by_name('Sheet')时候,
报错提示:
File "__main__",...

就是他们比较希望你使用这个方法wb
把ws = wb.get_sheet_by_name('Sheet') 换成 ws=wb['Sheet'],就不会跳出提示了

Gavin_coder 发表于 2019-12-31 10:33:43

111
页: 1 2 3 4 5 6 7 8 [9] 10 11 12 13 14 15 16 17 18
查看完整版本: 使用Python读写Excel文件(2)