搞懂Python对图片和文档的基本操作
名称 | RGBA值 | 名称 | RGBA值 |
White | (255, 255, 255, 255) | Red | (255, 0, 0, 255) |
Green | (0, 255, 0, 255) | Blue | (0, 0, 255, 255) |
Gray | (128, 128, 128, 255) | Yellow | (255, 255, 0, 255) |
Black | (0, 0, 0, 255) | Purple | (128, 0, 128, 255) |
pip install pillow
>>> from PIL import Image
>>>
>>> image = Image.open('./uploads/1.jpg')
>>> image.format, image.size, image.mode
('JPEG', (500, 750), 'RGB')
>>> image.show()
>>> image = Image.open('./uploads/1.jpg')
>>> rect = 80, 20, 310, 360
>>> image.crop(rect).show()
left:与左边界的距离
up:与上边界的距离
right:还是与左边界的距离
below:还是与上边界的距离
>>> image = Image.open('./uploads/1.jpg')
>>> size = 128, 128
>>> image.thumbnail(size)
>>> image.show()
>>> image1 = Image.open('./uploads/1.jpg')
>>> image2 = Image.open('./uploads/2.jpg')
>>> rect = 80, 20, 310, 360
>>> guido_head = image2.crop(rect)
>>> width, height = guido_head.size
>>> image1.paste(guido_head.resize((int(width / 1.5), int(height / 1.5))), (172, 40))
>>> image = Image.open('./res/guido.png')
>>> image.rotate(180).show()
>>> image.transpose(Image.FLIP_LEFT_RIGHT).show()
>>> image = Image.open('./res/guido.jpg')
>>> for x in range(80, 310):
... for y in range(20, 360):
... image.putpixel((x, y), (128, 128, 128))
...
>>> image.show()
>>> from PIL import Image, ImageFilter
>>>
>>> image = Image.open('./res/guido.jpg')
>>> image.filter(ImageFilter.CONTOUR).show()
import xlwt
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('animal')
# 写入excel
# 参数对应 行, 列, 值
worksheet.write(1,0, label = 'cat')
# 保存
workbook.save('test.xlsx')
from openpyxl import load_workbook
wb = load_workbook(filename = 'test.xlsx')
sheet_ranges = wb['range names']
print(sheet_ranges['D18'].value)
cat
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
worksheet.write(0, 0,'cat')
# 设置单元格宽度
worksheet.col(0).width = 3333
workbook.save('test.xlsx')
import xlwt
import datetime
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
style = xlwt.XFStyle()
style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0
worksheet.write(0, 0, datetime.datetime.now(), style)
workbook.save('test.xlsx')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
worksheet.write(0, 0, 5) # Outputs 5
worksheet.write(0, 1, 2) # Outputs 2
worksheet.write(1, 0, xlwt.Formula('A1*B1')) # Should output "10" (A1[5] * A2[2])
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # Should output "7" (A1[5] + A2[2])
workbook.save('test.xlsx')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.baidu.com";"baidu")')) # Outputs the text "Google" linking to http://www.baidu.com
workbook.save('test.xlsx')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.
font = xlwt.Font() # Create Font
font.bold = True # Set font to Bold
style = xlwt.XFStyle() # Create Style
style.font = font # Add Bold Font to Style
worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.
workbook.save('test.xlsx')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
alignment = xlwt.Alignment() # Create Alignment
alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
style = xlwt.XFStyle() # Create Style
style.alignment = alignment # Add Alignment to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('test.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('animal')
borders = xlwt.Borders() # Create Borders
borders.left = xlwt.Borders.DASHED
# DASHED虚线,NO_LINE没有,THIN实线
# May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style = xlwt.XFStyle() # Create Style
style.borders = borders # Add Borders to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('test.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
pattern = xlwt.Pattern() # Create the Pattern
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
style = xlwt.XFStyle() # Create the Pattern
style.pattern = pattern # Add Pattern to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('Excel_Workbook.xlsx')
import docx
#打开demo.docx文档,返回一个Document对象,它有paragraphs属性时Paragraph对象的列表。
doc = docx.Document('test.docx')
print(len(doc.paragraphs))
print(doc.paragraphs[0].text)
print(doc.paragraphs[1].text)
#每个Paragraph对象也有一个runs属性,它是Run对象的列表。
print(len(doc.paragraphs[1].runs))
#Run对象也有一个text属性,包含那个延续的文本
print(doc.paragraphs[1].runs[0].text)
import docx
def getText(filename):
doc = docx.Document(filename)
fullText = []
for para in doc.paragraphs:
fullText.append(para.text)
return '\n'.join(fullText)
print(getText('demo.docx'))
段落样式可以应用于Paragraph对象;
字符样式可以应用于Run对象;
链接的样式可以应用于这两种对象。
import docx
doc = docx.Document('demo.docx')
doc.paragraphs[0].text
>> 'Document Title'
doc.paragraphs[0].style
>> 'Title'
doc.paragraphs[0].style = 'Normal'
doc.paragraphs[1].runs[0].style = 'QuoteChar'
doc.paragraphs[1].runs[1].underline = True
doc.paragraphs[1].runs[3].underline = True
doc.save('restyle.docx')
from docx import Document
document = Document()
#直接添加标题
document.add_heading(u'Python 操作Word实例', 3)
#先定义标题,再添加内容
p_total = document.add_heading()
r_total = p_total.add_run("执行结果如下:")
r_total.font.bold = True
# 增加表格
table = document.add_table(rows=1, cols=3, style="Light List Accent 5")
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'testName'
hdr_cells[1].text = 'param'
hdr_cells[2].text = 'exc'
for i in range(5):
i = i + 1
testName = "testName"+str(i)
#img_name = "./test.jpg"
param = "param"+str(i)
exc="exc"+str(i)
row_cells = table.add_row().cells
row_cells[0].text = testName
row_cells[1].text = param
row_cells[2].text = exc
#添加二级标题
p_total = document.add_heading("", 2)
r_total = p_total.add_run(testName)
r_total.font.bold = True
# 保存文档
document.save("./test.docx")
import PyPDF2
pdfFileObj = open('meetingminutes.pdf','rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# pdf内容多少页
print(pdfReader.numPages)
pageObj = pdfReader.getPage(0)
# 读取pdf内容
pageObj.extractText()
import PyPDF2
pdfFile = open('encrypted.pdf','rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
# 返回True说明时加密的PDF
print(pdfReader.isEncrypted)
#调用decrypt()函数,传入口令字符串,返回1说明口令正确,之后就可以进行读取操作了
pdfReader.decrypt('rosebud')
import PyPDF2
pdfFile = open('test.pdf','rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
#我们将meetingminutes.pdf的页面拷贝到PdfFIleWriter对象
for pageNum in range(pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
#用口令swordfish加密了PdfFileWriter对象
pdfWriter.encrypt('swordfish')
#打开了一个名为encryptminutes.pdf的新PDF,将PdfFileWriter的内容写入新的PDF
resultPdf = open('test1.pdf','wb')
pdfWriter.write(resultPdf)
resultPdf.close()
import PyPDF2
#打开PDF,创建File对象,还有创建PdfFileReader对象,从打开的PDF中读取数据
pdf1File = open('test1.pdf','rb')
pdf2File = open('test2.pdf','rb')
pdf1Reader = PyPDF2.PdfFileReader(pdf1File)
pdf2Reader = PyPDF2.PdfFileReader(pdf2File)
#PdfFileWriter对象,表示一个空白的PDF文档,可以往里面写数据
pdfWriter = PyPDF2.PdfFileWriter()
#通过页面循环写入到pdfWriter对象中,pdf1Reader.getPage(pageNum)返回的就是该页面的内容
for pageNum in range(pdf1Reader.numPages):
pageObj = pdf1Reader.getPage(pageNum)
pdfWriter.addPage(pageObj)
for pageNum in range(pdf2Reader.numPages):
pageObj = pdf2Reader.getPage(pageNum)
pdfWriter.addPage(pageObj)
#通过open()新建一个PDF文档,然后把pdfWriter的内容写入到pdfOutputFile中。
pdfOutputFile = open('test3.pdf','wb')
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()
pdf1File.close()
pdf2File.close()