会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 128778个问题
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 121楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 122楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 123楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 124楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 125楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 126楼

import xlrd
from xlutils.copy import copy

def read_data():
    wb=xlrd.open_workbook('./base_data/data01.xlsx')
    sh=wb.sheet_by_index(0)
    fen_type={}
    count_price=[]

    for r in range(sh.nrows):
        count=sh.cell_value(r,3)
        count_price.append(count)
        key=sh.cell_value(r,0)
        if fen_type.get(key):
            fen_type[key]+=count
        else:
            fen_type[key]=count
    return fen_type,count_price

def save(fen,count):
    wb=xlrd.open_workbook('./base_data/data01.xlsx')
    sh_t=wb.sheet_by_index(0)
    wb2=copy(wb)
    sh=wb2.get_sheet(0)
    for r in range(sh_t.nrows):
        sh.write(r,sh_t.ncols,count[r])
    
    sh2=wb2,add_sheet('汇总数据')
    for i,key in enumerate(fen.keys()):
        sh2.write(i,0,key)
        sh2.write(i,1,fen.get(key))
    wb2.save('05_汇总数据.xlsx')

if __name__=="__main__":
    f,c=read_data()
    save(f,c)

PS D:\菁菁小私库\VScode> & "C:/Program Files (x86)/Python38-32/python.exe" d:/菁菁小私库/VScode/office办公自动化/05_统计数据.py

Traceback (most recent call last):

  File "d:/菁菁小私库/VScode/office办公自动化/05_统计数据.py", line 35, in <module>

    f,c=read_data()

  File "d:/菁菁小私库/VScode/office办公自动化/05_统计数据.py", line 5, in read_data

    wb=xlrd.open_workbook('./base_data/data01.xlsx')

  File "C:\Users\旸kn\AppData\Roaming\Python\Python38\site-packages\xlrd\__init__.py", line 170, in open_workbook

    raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')

xlrd.biffh.XLRDError: Excel xlsx file; not supported

PS D:\菁菁小私库\VScode> 


Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 127楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 128楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 129楼

一、代码

# pip install openpyxl

def open():
    # 导入模块  load_workbook
    from openpyxl import load_workbook
    # 打开excel
    wb = load_workbook('./base_data/data01.xlsx')
    # 获取工作薄的几种方式
    sh1 = wb.active
    sh2 = wb['Sheet1']
    sh3 = wb.get_sheet_by_name('Sheet1')
    
    print(sh1 is sh2 is sh3)

if __name__ == "__main__":
    open()

二、问题:为什么代码和老师教学视频里一样,但是却出现错误呢?

PS E:\Class\办公自动化> & "C:/Program Files/Python39/python.exe" e:/Class/办公自动化/07_openpyxl读取数据.py
Traceback (most recent call last):
  File "e:\Class\办公自动化\07_openpyxl读取数据.py", line 16, in <module>
    open()
  File "e:\Class\办公自动化\07_openpyxl读取数据.py", line 7, in open
    wb = load_workbook('./base_data/data01.xlsx')
  File "C:\Users\皮蛋丸子\AppData\Roaming\Python\Python39\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "C:\Users\皮蛋丸子\AppData\Roaming\Python\Python39\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "C:\Users\皮蛋丸子\AppData\Roaming\Python\Python39\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Program Files\Python39\lib\zipfile.py", line 1257, in __init__
    self._RealGetContents()
  File "C:\Program Files\Python39\lib\zipfile.py", line 1324, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file


Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 130楼

老师的源代码运行报错:

from sys import hexversion


def base_use():
    from pptx import Presentation
    from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
    from pptx.util import Inches, Pt
    ppt = Presentation()
    slide = ppt.slides.add_slide(ppt.slide_layouts[5])
    shapes = slide.shapes
    shapes.title.text = '流程图'

    top = Inches(3)
    left = Inches(1)
    width = Inches(2)
    height = Inches(1)
    t_sh = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON, left, top, width, height)
    t_sh.text = '第1步'

    for i in range(2, 6):
        left = left + width - Inches(0.4)
        temp = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON, left, top, width, height)
        # temp.text = f'第{i}步'
        p = temp.text_frame
        p.text = f'第{i}步'
        p.fit_text(max_size=10, bold=True, italic=True)

    ppt.save('28_增加流程图.pptx')


if __name__ == "__main__":
    base_use()

错误信息

F:\Python\virtualenv\work_env\Scripts\python.exe F:/Pycharm/14、自动化办公/PPT/test.py
Traceback (most recent call last):
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\util.py", line 137, in get_prop_value
    return getattr(obj, cache_attr_name)
AttributeError: '_Font' object has no attribute '__tables'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/Pycharm/14、自动化办公/PPT/test.py", line 32, in <module>
    base_use()
  File "F:/Pycharm/14、自动化办公/PPT/test.py", line 26, in base_use
    p.fit_text(max_size=10, bold=True, italic=True)
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\text.py", line 88, in fit_text
    font_size = self._best_fit_font_size(
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\text.py", line 242, in _best_fit_font_size
    font_file = FontFiles.find(family, bold, italic)
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 31, in find
    cls._font_files = cls._installed_fonts()
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 43, in _installed_fonts
    for key, path in cls._iter_font_files_in(d):
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 74, in _iter_font_files_in
    yield ((f.family_name, f.is_bold, f.is_italic), path)
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 155, in family_name
    return self._tables["name"].family_name
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\util.py", line 139, in get_prop_value
    value = f(obj)
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 185, in _tables
    return dict(
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 185, in <genexpr>
    return dict(
  File "F:\Python\virtualenv\work_env\lib\site-packages\pptx\text\fonts.py", line 177, in _iter_table_records
    yield tag.decode("utf-8"), off, len_
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte

Process finished with exit code 1


Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 131楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 133楼

百战程序员微信公众号

百战程序员微信小程序

©2014-2024 百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637