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)
XML Files
Create an XML file from scratch:
import xml.etree.ElementTree as ETroot = ET.Element("catalog")for title, price in [("Widget", "25.00"), ("Gadget", "49.99")]: item = ET.SubElement(root, "item") ET.SubElement(item, "title").text = title ET.SubElement(item, "price").text = priceoutput = Opus.file.create("catalog", "XML")tree = ET.ElementTree(root)ET.indent(tree)with output.open("wb") as f: tree.write(f, encoding="utf-8", xml_declaration=True)file_url = Opus.file.upload(output)
Parse a downloaded XML file:
import xml.etree.ElementTree as ETfile = Opus.file.download("https://files.opus.com/data/catalog.xml")tree = ET.parse(file.open("r"))root = tree.getroot()for item in root.findall("item"): title = item.find("title").text price = item.find("price").text print(f"{title}: ${price}")
Download an XML, modify it, and re-upload:
import xml.etree.ElementTree as ETfile = Opus.file.download("https://files.opus.com/data/config.xml")tree = ET.parse(file.open("r"))root = tree.getroot()# Update a settingsetting = root.find(".//setting[@name='timeout']")if setting is not None: setting.set("value", "60")ET.indent(tree)with file.open("wb") as f: tree.write(f, encoding="utf-8", xml_declaration=True)new_url = Opus.file.upload(file)
XLS & XLSX Files
Use openpyxl to read and write XLSX spreadsheets.Create an XLSX from scratch:
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)
PDF Files
Three PDF libraries are available: fitz (PyMuPDF), fpdf2, and PyPDF2.
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)
Related
Overview
Learn the core file API: create, download, and upload.
Available Packages
See every Python library you can import in Opus Code.
Opus Code
Run these recipes in a Python task inside your workflow.
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.