-
Py chrome常用快捷鍵大全
——————《Python辦公自動化》查看全部 -
# 添加表格
table = slide.shapes.add_table(2, 3, Inches(2), Inches(2), Inches(4), Inches(2)).table
# 填充內容
table.cell(0, 0).text = 'title1'
table.cell(0, 1).text = 'title2'
table.cell(0, 2).text = 'title3'
# 合并單元格
cell = table.cell(0, 0)
cell1 = table.cell(0, 2)
cell.merge(cell1)
# 取消合并單元格
if cell.is_merge_origin:
????cell.split()
查看全部 -
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
# 添加自選圖形
shape = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(2), Inches(2), Inches(5), Inches(3))
# 設置填充色、邊框樣式
fill = shape.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 0, 0)
line = shape.line
line.color.rgb = RGBColor(55, 3, 5)
line.width = Pt(2)
查看全部 -
import pptx
from pptx.util import Inches
# 得到演示文稿的對象
prs = pptx.Presentation()
# 打開ppt
# prs = pptx.Presentation('test.pptx')
# 指定模板,插入一張幻燈片
slide = prs.slides.add_slide(prs.slide_layouts[0])
prs.slides.add_slide(prs.slide_layouts[1])
prs.slides.add_slide(prs.slide_layouts[2])
# 刪除幻燈片
del prs.slides._sldIdLst[1]
# 向幻燈片中插入文本框
text1 = slide.shapes.add_textbox(Inches(5), Inches(5), Inches(5), Inches(5))
text1.text = '我是文本框'
p1 = text1.text_frame.add_paragraph()
p1.text = '我是段落1'
p1.add_run().text = 'end'
# 向幻燈片中現有的元素寫入文本
title_shape = slide.shapes.title
title_shape.text = '標題1'
slide.shapes.placeholders[1].text = '標題2'
# 保存ppt文檔
prs.save('test.pptx')
查看全部 -
pip install python-pptx
import pptx
查看全部 -
pip install pywin32
from win32com.client import constants, gencache
import os
def createpdf(wordPath, pdfPath):
????word = gencache.EnsureDispatch('Word.Application'
????doc = word.Documents.Open(wordPath, ReadOnly=1)
????# 轉換方法
????doc.ExportAsFixedFormat(pdfPath, constants.wdExportFormatPDF)
????word.Quit()
# 單個文件轉換
createpdf('info.docx', 'info.pdf')
# 多個文件轉換
os.listdir('.') # 獲取當前文件夾下的所有文件
wordfiles = []????# 存儲當前文件夾下的所有word文件
for file in os.listdir('.'):
????if file.endswith(('.doc', '.docx')):
????????wordfiles.append(file)
for file in wordfiles:
????filepath = os.path.abspath(file)????# 獲取文件的絕對路徑
????index = filepath.rindex('.') # 獲取最后一個點的索引
????pdfpath = filepath[:index] + '.pdf' # 構造pdf絕對路徑
????createpdf(filepath, pdfpath)
查看全部 -
from docx.enum.style import WD_STYLE_TYPE # 導入
style = document.styles.add_style('textstyle', WD_STYLE_TYPE.PARAGRAPH) # 創建段落樣式
style.font.size = Pt(5) # 設置字體大小
p1 = document.add_paragraph('texttexttext', style='textstyle') # 為段落設置樣式
官方文檔:https://python-docx.readthedocs.io/en/latest/user/styles-using.html
樣式對照表:http://www.thedoctools.com/index.php?show=mt_create_style_name_list
table = document.add_table(rows=1, cols=3, style='Medium List 1') # 為表格設置內置樣式
document.styles['textstyle'].delete() # 刪除樣式
查看全部 -
document.add_picture('logo.jpg') # 插入圖片
document.add_picture('logo.jpg', Pt(20), Pt(30)) # 插入圖片,設置寬高
table = document.add_table(rows=1, cols=2) # 插入表格
header_cells = table.rows[0].cells # 獲取第一行的單元格
header_cells[0].text = 'title' # 向表格中寫入內容
rows.cells = table.add_row().cells # 添加行
len(document.tables[0].rows) # 獲取文檔中第一個表格的總行數
document.tables[0].cell(0, 1).text # 獲取文檔中第一個表格的第一行第二列的內容
查看全部 -
from docx import Document
from docx.shared import Pt, RGBColor
document = Document() # 創建文檔對象
document = Document("info.docx") # 讀取現有word,創建文檔對象
document.add_heading('title') # 寫入標題,默認標題1樣式
document.add_heading('title', level=4) # 指定標題級別
p1 = document.add_paragraph('xxxxxxxxxxxxxxxxx') # 寫入段落
p1.insert_paragraph_before('xxxxxxxxxx') # 在段落前插入段落
format = p1.paragraph_format # 獲取段落的樣式
format.left_indent = Pt(20) # 左側縮進
format.right_indent = Pt(20) # 右側縮進
format.first_line_indent = Pt(20) # 首行縮進
format.line_spacing = 1.5 # 1.5倍行間距
run = p1.add_run('xxxxxxxxxx') # 在段落后追加文字
run.font.size = Pt(12) # 設置字號
run.font.name =?'微軟雅黑' # 設置字體
run.font.color.rgb = RGBColor(235, 33, 24) # 設置文字顏色
run.bold = True # 設置加粗
run.font.underline = True # 添加下劃線
run.font.italic = True # 設置斜體
document.save('test.docx') # 保存文檔
查看全部 -
python-docx
查看全部 -
import xlsxwriter
wb = xlsxwriter.Workbook("data.xlsx") # 創建工作簿
sheet = wb.add_worksheet("newsheet") # 創建工作表
sheet.write(0, 0, "title") # 寫入單元格數據
sheet.merge_range(1, 0, 2, 2, "data") # 合并單元格并寫入數據
sheet.write_row(3, 0, ["col1", "col2", "col3" ]
sheet.write(7, 1, "=sum(B5:B7)" # 寫入公式
sheet.write_url(9, 0, ", string = "更多數據" # 寫入超鏈接
sheet.insert_image(10, 0, "view.png") # 插入圖片
wb.close() # 關閉文件
查看全部 -
style = xlwt.XFStyle() # 初始化樣式
font = xlwt.Font() # 初始化字體
font.name = "宋體" # 字體名稱
font.bold = True # 加粗
font.height = 11 * 20 # 字號設置為11,20為衡量單位
font.colour_index = 0x08 # 字體顏色設置為黑色,也可等于索引1
style.font = font # 設置字體
align = xlwt.Alignment() # 初始化對齊方式
align.horz = 0x02 # 設置水平方向居中對齊
align.vert = 0x01 # 設置垂直方向居中對齊
style.alignment = align # 設置對齊方式
borders = xlwt.Borders() # 初始化邊框
borders.right = xlwt.Borders.DASHED # 右邊框設置為虛線
borders.bottom = xlwt.Borders.DOTTED # 下邊框設置為點線
style.borders = borders # 設置邊框樣式
bgcolor = xlwt.Pattern() # 初始化顏色模式
bgcolor.pattern = xlwt.Pattern.SOLID_PATTERN # 顏色模式設置為背景顏色
bgcolor.pattern_fore_colour = 22 # 背景顏色設置為灰色
style.pattern = bgcolor # 設置背景顏色
ws.write(0, 0, value, style) # 設置單元格樣式
查看全部 -
import xlwt # 導入模塊,只支持xls格式
wb = xlwt.Wortbook() # 創建工作簿
ws = wb.add_sheet("sheet1") # 創建sheet
ws.write_merge(0, 1, 0, 5, "title") # 合并單元格,并寫入數據
ws.write(i + 2, j, value) # 向單元格寫入數據
ws.insert_bimap("文件路徑.bmp", 0, 1) # 插入圖片
wb. save("保存文件路徑.xls") # 保存工作簿
查看全部 -
pip install xlrd
查看全部 -
使用pycharm
pycharm常用配置
ctrl+shift+F10 運行
文件和代碼片段? #author:username #createdate:${DATE}
查看全部
舉報