Last active
June 4, 2018 12:59
-
-
Save wwex/3762409ac5f171b1a3463acfdf697fd8 to your computer and use it in GitHub Desktop.
[PyMemos - Excel, PDF, docs] #python #memo #excel #pdf #doc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import docx | |
d = docx.Document('demo.docx') | |
p = d.paragraphs[1] | |
print(p.text) | |
print(p.runs[1].bold) | |
print(p.runs[3].italic) | |
p.runs[3].underline = True | |
p.runs[3].text = 'italic and underlined' | |
p.style = "Title" | |
d.save('demo2.docx') | |
d = docx.Document() | |
d.add_paragraph('Hello, this is paragraph.') | |
d.add_paragraph('Andthis is another paragraph.') | |
d.save('demo3.docx') | |
def getDocText(filename): | |
d = docx.Document(filename) | |
fullText = [] | |
for par in d.paragraphs: | |
fullText.append(par.text) | |
return '\n'.join(fullText) | |
print(getDocText('demo.docx')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openpyxl | |
# open and work with an existing excel workbook | |
os.chdir(r'C:\Users\woj.wojciechowski\Desktop\py') | |
workbook = openpyxl.load_workbook('exampleABS.xlsx') | |
print(workbook.sheetnames) | |
sheet = workbook['Sheet1'] | |
cell = sheet['A1'] | |
print(cell.value) | |
print(sheet.cell(row=1, column=3).value) | |
# open and work new workbook | |
wb = openpyxl.Workbook() | |
print(wb.sheetnames) | |
sheet = wb['Sheet'] | |
sheet['A1'].value = 6 | |
sheet['A2'].value = 12 | |
sheet2 = wb.create_sheet(index=0, title='New_sheet') | |
print(wb.sheetnames) | |
sheet2.title = 'Sheet2' | |
print(wb.sheetnames) | |
os.chdir(r'C:\Users\woj.wojciechowski\Desktop\py') | |
wb.save('exampleABS2.xlsx') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PyPDF2 | |
os.chdir(r'C:\Users\woj.wojciechowski\Desktop\py') | |
pdfFile = open('meetingminutes1.pdf', 'rb') | |
reader = PyPDF2.PdfFileReader(pdfFile) | |
print(reader.numPages) | |
page = reader.getPage(0) | |
print(page.extractText()) | |
for pageNum in range(reader.numPages): | |
print(reader.getPage(pageNum).extractText()) | |
writer = PyPDF2.PdfFileWriter() | |
for pageNum in range(reader.numPages): | |
page = reader.getPage(pageNum) | |
writer.addPage(page) | |
writer.addPage(page) # duplikuje sobie bo tak | |
outputFile = open('duplicatedPdf.pdf', 'wb') | |
writer.write(outputFile) | |
outputFile.close() | |
pdfFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment