其他分享
首页 > 其他分享> > day20-自动化办公2

day20-自动化办公2

作者:互联网

day20-自动化办公2

1.excel 文件的读操作

import openpyxl
(1) 加载文件(只能加载后缀是.xlsx文件)
wb = openpyxl.load_workbook('files/学生信息.xlsx')
(2) 从工作簿中获取工作表相关信息
names = wb.sheetnames
print(names)
sheet1 = wb.active
print(sheet1)
sheet2 = wb['sheet1']
print(sheet2)
(3) 从工作表中获取表相关内容
print(sheet1.title)      # 'sheet1'
print(sheet1.max_row)
print(sheet1.max_column)
(4) 通过表获取单元格
cell1 = sheet1['A2']
print(cell1)
cell2 = sheet1.cell(2, 1)
print(cell1, value)
print(cell2, value)

2.excel 文件写操作

(1) 新建工作簿
if os.path.exists('files/test.xlsx')
	print('打开')
    wb = openpyxl.load_workbook('files/test.xlsx')
else:
    print('新建')
    wb = openpyxl.Workbook()
    wb.save('files/test.xlsx')
(2) 保存工作簿
wb.save('files/test.xlsx')
(3) 针对表的写操作
wb.create_sheet('老师表')
wb.create_sheet('学生表', 0)
try:
    wb.remove(wb['老师表'])
except KeyError:
    pass
if 'Sheet' in wb.sheetnames:
    wb['Sheet'].title = '课程表'
(4) 修改单元格中的内容
sheet1 = wb.active
sheet1['A1'] = '姓名'
sheet1['B1'] = '年龄'
sheet1['A1'] = ''

sheet1.cell(1, 2).value = 'Age'
print(sheet1.cell(1, 2).value)

3.csv 文件操作

(1) 创建文件对象的 reader 对象
f = open('files/2018年北京积分落户数据.csv', encoding='utf-8')
# reader = csv.reader(f)
reader = csv.DictReader(f)
print(next(reader))
print(next(reader))
(2) 创建文件对应的 writer 对象
f = open('files/test.csv', 'w', encoding='utf-8')
writer = csv.writer(f)
f = open('files/test2.csv', 'w', encoding='utf-8')
writer = csv.DictWriter(f, ['name', 'age', 'score', 'gender'])

# 写数据
writer.writeheader()     # 将原来设定好的字典的键作为第一行内容写入文件
writer.writerow({'name': '小明', 'age': 18, 'score': 100, 'gender': '男'})
writer.writerows([
    {'name': '小花', 'age': 30, 'score': 92, 'gender': '女'},
    {'name': '小红', 'age': 28, 'score': 67, 'gender': '女'},
    {'name': '老王', 'age': 25, 'score': 88, 'gender': '男'}
])

5.pdf 文件操作

import PyPDF2, openpyxl
(1) 读操作 - 获取 pdf 文件内容
reader = PyPDF2.PdfFileReader('files/HEU_KMS_Activator_v20.0.0用户使用手册.pdf')
total_page = reader.getNumPages()
print(total_page)
page0 = reader.getPage(0)
page1 = reader.getPage(1)
page2 = reader.getPage(2)
page3 = page1.rotateClockwise(90)    # 旋转
(2) 写操作
writer1 = PyPDF2.PdfFileWriter()
writer1.addPage(page0)
writer1.addBlankPage()
writer1.addPage(page1)
writer1.addBlankPage()
writer1.addPage(page2)
writer1.addBlankPage()
f = open('files/test.pdf', 'wb')
writer1.write(f)

6. 添加水印

import PyPDF2

# 1. 准备需要添加水印的pdf
reader1 = PyPDF2.PdfFileReader('files/存储引擎的讲解.pdf')
# 2. 准备水印文件
walter_page = PyPDF2.PdfFileReader('files/demo1.pdf').getPage(0)
# 3. 准备空的pdf,用来存放添加完水印的页
new_pdf = PyPDF2.PdfFileWriter()
# 4. 遍历原文件的每页内容,添加水印后添加到空的pdf中
for page_num in range(reader1.getNumPages()):
    # 获取原文件的一页内容
    page = reader1.getPage(page_num)
    # 添加水印
    page.mergePage(walter_page)
    # 将新的页添加到空的pdf中
    new_pdf.addPage(page)
# 5.保存新的pdf文件
f = open('files/test3.pdf', 'wb')
new_pdf.write(f)

7.创建文印文件

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas

# 1. 注册字体文件
pdfmetrics.registerFont(TTFont('Font1', 'files/Vera.ttf'))
pdfmetrics.registerFont(TTFont('Font2', 'files/青呱石头体.ttf'))

# 2.生成文字
# 1)创建空的pdf文件
walter_pdf = canvas.Canvas('files/walter.pdf')

# 2)设置字体
walter_pdf.setFont('Font1', 40)

# 3)设置文字颜色
walter_pdf.setFillColorRGB(1, 0, 0, 1)

# 4)旋转
walter_pdf.rotate(45)

# 5)渲染文字内容
walter_pdf.drawString(200, 100, 'hello world!')

walter_pdf.setFillColorRGB(0, 1, 0, 1)
walter_pdf.setFont('Font2', 40)
walter_pdf.drawString(400, 200, 'hello world!')


# 6)保存
walter_pdf.save()

标签:files,文件,wb,day20,办公,自动化,print,pdf,sheet1
来源: https://blog.csdn.net/qq_57748947/article/details/119463286