1.excel文件的读操作
import openpyxl
wb = openpyxl.load_workbook('files/小宝剑大药房.xlsx')
names = wb.sheetnames
print(names)
sheet1 = wb.active
print(sheet1)
sheet2 = wb['Sheet1']
print(sheet2)
print(sheet1.title)
print(sheet1.max_row)
print(sheet1.max_column)
cell1 = sheet1['B2']
print(cell1)
cell2 = sheet1.cell(2,2)
print(cell1.value)
print(cell2.value)
for i in range(1, sheet1.max_row+1):
for j in range(1, sheet1.max_column+1):
cell3 = sheet1.cell(i, j)
print(cell3.value,end=' ')
print()
for j in range(1, sheet1.max_column+1):
cell3 = sheet1.cell(2, j)
print(cell3.value,end=' ')
for i in range(1, sheet1.max_row+1):
cell3 = sheet1.cell(i, 3)
print(cell3.value,end=' ')
2.excel 文件的写操作
import openpyxl
import os
if os.path.exists('files/test.xlsx'):
print('打开')
wb = openpyxl.load_workbook('files/test.xlsx')
else:
wb = openpyxl.Workbook()
wb.save('files/test.xlsx')
if 'Sheet' in wb.sheetnames:
wb['Sheet'].title = '课程表'
sheet1 = wb.active
3.csv文件读操作
import csv
f = open('files/douyin.csv', encoding='utf-8')
reader = csv.DictReader(f)
print(next(reader))
print(next(reader))
f.close()
'''
f = open('files/test1.csv', 'w', encoding='utf-8')
writer = csv.writer(f)
writer.writerow(['name','age','score','gender'])
writer.writerows([
['小明',18,100,'男'],
['小花',18,100,'女']
])
f = open('files/test2.csv','w',encoding='utf-8')
writer = csv.DictWriter(f,['name','age','score','gender'])
writer.writeheader()
writer . writerows([
{'name': '小花','age': 30,'score': 92,'gender':'女'},
{'name': '小红','age': 28,'score': 67,'gender':'女'},
{'name': '老王','age': 25,'score': 88,'gender':'男'}
])
f.close()
5.PDF文件操作
import PyPDF2
reader = PyPDF2.PdfFileReader('files/python.pdf')
total_page = reader.getNumPages()
print(total_page)
page0 = reader.getPage(0)
page1 = reader.getPage(1)
page2 = reader.getPage(2)
page3 = page1.rotateClockwise(90)
write1 = PyPDF2.PdfFileWriter()
write1.addPage(page0)
write1.addBlankPage()
write1.addPage(page1)
write1.addBlankPage()
write1.addPage(page2)
write1.addBlankPage()
write1.addPage(page3)
f = open('files/test.pdf','wb')
write1.write(f)
f.close()
PDF文件添加水印
import PyPDF2
reader = PyPDF2.PdfFileReader('files/存储引擎的讲解.pdf')
walter_page = PyPDF2.PdfFileReader('files/water.pdf').getPage(0)
new_pdf = PyPDF2.PdfFileWriter()
for page_num in range(reader.getNumPages()):
page = reader.getPage(page_num)
page.mergePage(walter_page)
new_pdf.addPage(page)
f = open('files/存储引擎的讲解水印版.pdf','wb')
new_pdf.write(f)
f.close()
6.创建一个水印的PDF文档
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
pdfmetrics.registerFont(TTFont('Font1','files/Vera.ttf'))
pdfmetrics.registerFont(TTFont('Font2','files/青呱石头体.ttf'))
walter_pdf = canvas.Canvas('files/water.pdf')
walter_pdf.setFont('Font1',40)
walter_pdf.setFillColorRGB(1,0,0,1)
walter_pdf.rotate(45)
walter_pdf.drawString(400,100,'hello world!')
walter_pdf.save()
7.Excel文件转CSV文件
import openpyxl
import csv
wb = openpyxl.load_workbook('files/小宝剑大药房.xlsx')
sheet1 = wb.active
big_list = []
for i in range(1, sheet1.max_row+1):
list1 = []
for j in range(1, sheet1.max_column+1):
cell3 = sheet1.cell(i, j)
list1.append(cell3.value)
big_list.append(list1)
f = open('files/test3.csv', 'w', encoding='utf-8')
writer = csv.writer(f)
writer.writerows(big_list)
f.close()
8.合并两个PDF文档
import PyPDF2
reader1 = PyPDF2.PdfFileReader('files/python.pdf')
reader2 = PyPDF2.PdfFileReader('files/demo1.pdf')
total_page1 = reader1.getNumPages()
total_page2 = reader2.getNumPages()
write1 = PyPDF2.PdfFileWriter()
for i in range(total_page1):
page = reader1.getPage(i)
write1.addPage(page)
for i in range(total_page2):
page = reader2.getPage(i)
write1.addPage(page)
f = open('files/合并python和demo1.pdf','wb')
write1.write(f)
f.close()