Recipes for reading and writing CSV, XML, XLSX, PDF, DOCX, and PPTX files with the Opus SDK.
These recipes build on the core file API described in Overview and use the libraries listed in Available Packages. Each example runs in an Opus Code task, where Opus.file.create, Opus.file.download, and Opus.file.upload give you everything you need to read, transform, and produce files.
import csvfile = Opus.file.download("https://files.opus.com/data/employees.csv")with file.open("r") as f: reader = csv.DictReader(f) for row in reader: if int(row["score"]) > 30: print(row["name"], row["score"])
Download a CSV, filter rows, and upload as a new file:
import csvsource = Opus.file.download("https://files.opus.com/data/scores.csv")output = Opus.file.create("high_scores", "CSV")with source.open("r") as src, output.open("w") as dst: reader = csv.DictReader(src) writer = csv.DictWriter(dst, fieldnames=reader.fieldnames) writer.writeheader() for row in reader: if float(row["score"]) > 90: writer.writerow(row)file_url = Opus.file.upload(output)
from openpyxl import load_workbookfile = Opus.file.download("https://files.opus.com/data/report.xlsx")wb = load_workbook(file.open("rb"))ws = wb.activeheaders = [cell.value for cell in ws[1]]for row in ws.iter_rows(min_row=2, values_only=True): data = dict(zip(headers, row)) if data["quantity"] > 50: print(data["product"], data["quantity"])
Download an XLSX, modify it, and re-upload:
from openpyxl import load_workbookfile = Opus.file.download("https://files.opus.com/data/report.xlsx")wb = load_workbook(file.open("rb"))ws = wb.active# Add a total rowtotal = sum(row[2] for row in ws.iter_rows(min_row=2, values_only=True))ws.append(["Total", "", total])with file.open("wb") as f: wb.save(f)new_url = Opus.file.upload(file)
Convert a downloaded XLSX to CSV:
import csvfrom openpyxl import load_workbookfile = Opus.file.download("https://files.opus.com/data/report.xlsx")wb = load_workbook(file.open("rb"))ws = wb.activeoutput = Opus.file.create("report", "CSV")with output.open("w") as f: writer = csv.writer(f) for row in ws.iter_rows(values_only=True): writer.writerow(row)file_url = Opus.file.upload(output)
from docx import Documentfile = Opus.file.download("https://files.opus.com/data/report.docx")doc = Document(file.open("rb"))for para in doc.paragraphs: print(para.text)
Read tables from a downloaded DOCX:
from docx import Documentfile = Opus.file.download("https://files.opus.com/data/report.docx")doc = Document(file.open("rb"))for table in doc.tables: for row in table.rows: print([cell.text for cell in row.cells])
Use python-pptx to read and modify PowerPoint presentations.
PPTX is not a supported file type for Opus.file.create. You can download existing PPTX files, modify them, and re-upload.
Read text from all slides of a downloaded PPTX:
from pptx import Presentationfile = Opus.file.download("https://files.opus.com/data/presentation.pptx")prs = Presentation(file.open("rb"))for slide_num, slide in enumerate(prs.slides, 1): print(f"--- Slide {slide_num} ---") for shape in slide.shapes: if shape.has_text_frame: print(shape.text)
Download a PPTX and replace text across all slides:
from pptx import Presentationfile = Opus.file.download("https://files.opus.com/data/template.pptx")prs = Presentation(file.open("rb"))for slide in prs.slides: for shape in slide.shapes: if shape.has_text_frame: for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs: run.text = run.text.replace("{{COMPANY}}", "Acme Corp")prs.save(file.open("wb"))new_url = Opus.file.upload(file)